How to use if on a specific string












-1















I want to use an if statement to see if something has hidden written in it. The div classname is ".title-button" and I want to use in javascript or jquery if {.title-button = "Hidden"){...} but it doesn't work.



This is for a side project of mine. I have researched it lots of time but I cant find anything



if(".title-button" = "hidden"){
}


is the only code I have, but it doesn't work.



My expected result is to be able to check the string in the class, I don't get an actual result.










share|improve this question

























  • So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?

    – Taplar
    Jan 2 at 18:04











  • Use jQuery#hasClass: if($(".title-button").hasClass("hidden")) {

    – ibrahim mahrir
    Jan 2 at 18:04













  • I'm trying to see if the element contains the word "hidden"

    – flugs
    Jan 2 at 18:06











  • So you're trying to see if <div class="title-button">hidden</div> exists? That's what "contains the word" would mean

    – Taplar
    Jan 2 at 18:07













  • or if <div class="title-button">My Button</div> is visible (actually hidden) at the moment?

    – Steve0
    Jan 2 at 18:08
















-1















I want to use an if statement to see if something has hidden written in it. The div classname is ".title-button" and I want to use in javascript or jquery if {.title-button = "Hidden"){...} but it doesn't work.



This is for a side project of mine. I have researched it lots of time but I cant find anything



if(".title-button" = "hidden"){
}


is the only code I have, but it doesn't work.



My expected result is to be able to check the string in the class, I don't get an actual result.










share|improve this question

























  • So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?

    – Taplar
    Jan 2 at 18:04











  • Use jQuery#hasClass: if($(".title-button").hasClass("hidden")) {

    – ibrahim mahrir
    Jan 2 at 18:04













  • I'm trying to see if the element contains the word "hidden"

    – flugs
    Jan 2 at 18:06











  • So you're trying to see if <div class="title-button">hidden</div> exists? That's what "contains the word" would mean

    – Taplar
    Jan 2 at 18:07













  • or if <div class="title-button">My Button</div> is visible (actually hidden) at the moment?

    – Steve0
    Jan 2 at 18:08














-1












-1








-1








I want to use an if statement to see if something has hidden written in it. The div classname is ".title-button" and I want to use in javascript or jquery if {.title-button = "Hidden"){...} but it doesn't work.



This is for a side project of mine. I have researched it lots of time but I cant find anything



if(".title-button" = "hidden"){
}


is the only code I have, but it doesn't work.



My expected result is to be able to check the string in the class, I don't get an actual result.










share|improve this question
















I want to use an if statement to see if something has hidden written in it. The div classname is ".title-button" and I want to use in javascript or jquery if {.title-button = "Hidden"){...} but it doesn't work.



This is for a side project of mine. I have researched it lots of time but I cant find anything



if(".title-button" = "hidden"){
}


is the only code I have, but it doesn't work.



My expected result is to be able to check the string in the class, I don't get an actual result.







javascript jquery string if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 18:07









Shahnewaz

392312




392312










asked Jan 2 at 18:02









flugsflugs

214




214













  • So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?

    – Taplar
    Jan 2 at 18:04











  • Use jQuery#hasClass: if($(".title-button").hasClass("hidden")) {

    – ibrahim mahrir
    Jan 2 at 18:04













  • I'm trying to see if the element contains the word "hidden"

    – flugs
    Jan 2 at 18:06











  • So you're trying to see if <div class="title-button">hidden</div> exists? That's what "contains the word" would mean

    – Taplar
    Jan 2 at 18:07













  • or if <div class="title-button">My Button</div> is visible (actually hidden) at the moment?

    – Steve0
    Jan 2 at 18:08



















  • So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?

    – Taplar
    Jan 2 at 18:04











  • Use jQuery#hasClass: if($(".title-button").hasClass("hidden")) {

    – ibrahim mahrir
    Jan 2 at 18:04













  • I'm trying to see if the element contains the word "hidden"

    – flugs
    Jan 2 at 18:06











  • So you're trying to see if <div class="title-button">hidden</div> exists? That's what "contains the word" would mean

    – Taplar
    Jan 2 at 18:07













  • or if <div class="title-button">My Button</div> is visible (actually hidden) at the moment?

    – Steve0
    Jan 2 at 18:08

















So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?

– Taplar
Jan 2 at 18:04





So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?

– Taplar
Jan 2 at 18:04













Use jQuery#hasClass: if($(".title-button").hasClass("hidden")) {

– ibrahim mahrir
Jan 2 at 18:04







Use jQuery#hasClass: if($(".title-button").hasClass("hidden")) {

– ibrahim mahrir
Jan 2 at 18:04















I'm trying to see if the element contains the word "hidden"

– flugs
Jan 2 at 18:06





I'm trying to see if the element contains the word "hidden"

– flugs
Jan 2 at 18:06













So you're trying to see if <div class="title-button">hidden</div> exists? That's what "contains the word" would mean

– Taplar
Jan 2 at 18:07







So you're trying to see if <div class="title-button">hidden</div> exists? That's what "contains the word" would mean

– Taplar
Jan 2 at 18:07















or if <div class="title-button">My Button</div> is visible (actually hidden) at the moment?

– Steve0
Jan 2 at 18:08





or if <div class="title-button">My Button</div> is visible (actually hidden) at the moment?

– Steve0
Jan 2 at 18:08












3 Answers
3






active

oldest

votes


















3














If you are trying to check if the element has the text of hidden, then that would be something like.






if ($('.title-button').text() === 'hidden') {
console.log('yes');
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="title-button">hidden</div>





There are various issues with your original attempt:



if(".title-button" = "hidden"){}


1) You are not looking up the element



".title-button" is just a string. It's not an element lookup. To use jQuery to actually look the element up, you have to use $(".title-button").



2) = is assignment



Assignment is meant to be used with variables (ex. var x = 3), not comparison. Comparisons are performed with == or ===, the second one not performing type coersion.






share|improve this answer































    0














    Your current code says "set the string .title-button equal to the string hidden and, if that value is truthy (i.e. true, a nonzero number, a nonempty string, etc.), then do something". That makes no sense. So let's take this step-by-step.



    "My expected result is to be able to check the string in the class."



    What is "the string in the class"? A class name is a string, which simply indicates a group that an element belongs to. If you're trying to check whether an element which has the class title-button also has the class hidden, then you'd want to first get the element -- not just its class name -- and check all its classes for the class hidden. With jQuery, you can get an element from one of its classes by using $('.class-name'), so in this case, $('.title-button'). Then you can check if it has a certain class using the .hasClass('new-class-name)method. Soif ($('.title-button').hasClass('hidden')) {` is the condition you want to check.



    If instead you're trying to check if the contents of the element contain the string "hidden", then you need to first get the contents and then check if it contains what you want. You can do this a few ways, but here's just one: if ($('.title-button').html().indexOf('hidden') >= 0) { (For reference, the other methods instead of using indexOf would be .includes and .match; you could also use .test, though that has different syntax since the Regular Expression comes first, so I didn't show it here.)






    share|improve this answer































      0














      If you are trying to find the text "hidden" in the div elements content,for example



       <div class="title-button">I am hidden</div>

      If(document.querySelector(".title-button").innerHTML.indexOf("hidden") != -1){
      //Your statement goes here
      }


      If you are trying to find that the div element has class "hidden" in its class list,then there are two scenarios,



      1)If you have multiple classes on the div element.For example,



      <div class="title-button class2 class3"></div>

      If(document.querySelector(".title-button").className.indexOf("hidden") != -1){
      //Your statement goes here
      }


      2)If you have only one class as follows,



      <div class="title-button"></div>

      If(document.querySelector(".title-button").className == "hidden"){
      //Your statement goes here
      }





      share|improve this answer


























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011061%2fhow-to-use-if-on-a-specific-string%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        If you are trying to check if the element has the text of hidden, then that would be something like.






        if ($('.title-button').text() === 'hidden') {
        console.log('yes');
        }

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <div class="title-button">hidden</div>





        There are various issues with your original attempt:



        if(".title-button" = "hidden"){}


        1) You are not looking up the element



        ".title-button" is just a string. It's not an element lookup. To use jQuery to actually look the element up, you have to use $(".title-button").



        2) = is assignment



        Assignment is meant to be used with variables (ex. var x = 3), not comparison. Comparisons are performed with == or ===, the second one not performing type coersion.






        share|improve this answer




























          3














          If you are trying to check if the element has the text of hidden, then that would be something like.






          if ($('.title-button').text() === 'hidden') {
          console.log('yes');
          }

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <div class="title-button">hidden</div>





          There are various issues with your original attempt:



          if(".title-button" = "hidden"){}


          1) You are not looking up the element



          ".title-button" is just a string. It's not an element lookup. To use jQuery to actually look the element up, you have to use $(".title-button").



          2) = is assignment



          Assignment is meant to be used with variables (ex. var x = 3), not comparison. Comparisons are performed with == or ===, the second one not performing type coersion.






          share|improve this answer


























            3












            3








            3







            If you are trying to check if the element has the text of hidden, then that would be something like.






            if ($('.title-button').text() === 'hidden') {
            console.log('yes');
            }

            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <div class="title-button">hidden</div>





            There are various issues with your original attempt:



            if(".title-button" = "hidden"){}


            1) You are not looking up the element



            ".title-button" is just a string. It's not an element lookup. To use jQuery to actually look the element up, you have to use $(".title-button").



            2) = is assignment



            Assignment is meant to be used with variables (ex. var x = 3), not comparison. Comparisons are performed with == or ===, the second one not performing type coersion.






            share|improve this answer













            If you are trying to check if the element has the text of hidden, then that would be something like.






            if ($('.title-button').text() === 'hidden') {
            console.log('yes');
            }

            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <div class="title-button">hidden</div>





            There are various issues with your original attempt:



            if(".title-button" = "hidden"){}


            1) You are not looking up the element



            ".title-button" is just a string. It's not an element lookup. To use jQuery to actually look the element up, you have to use $(".title-button").



            2) = is assignment



            Assignment is meant to be used with variables (ex. var x = 3), not comparison. Comparisons are performed with == or ===, the second one not performing type coersion.






            if ($('.title-button').text() === 'hidden') {
            console.log('yes');
            }

            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <div class="title-button">hidden</div>





            if ($('.title-button').text() === 'hidden') {
            console.log('yes');
            }

            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <div class="title-button">hidden</div>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 18:13









            TaplarTaplar

            17.8k21529




            17.8k21529

























                0














                Your current code says "set the string .title-button equal to the string hidden and, if that value is truthy (i.e. true, a nonzero number, a nonempty string, etc.), then do something". That makes no sense. So let's take this step-by-step.



                "My expected result is to be able to check the string in the class."



                What is "the string in the class"? A class name is a string, which simply indicates a group that an element belongs to. If you're trying to check whether an element which has the class title-button also has the class hidden, then you'd want to first get the element -- not just its class name -- and check all its classes for the class hidden. With jQuery, you can get an element from one of its classes by using $('.class-name'), so in this case, $('.title-button'). Then you can check if it has a certain class using the .hasClass('new-class-name)method. Soif ($('.title-button').hasClass('hidden')) {` is the condition you want to check.



                If instead you're trying to check if the contents of the element contain the string "hidden", then you need to first get the contents and then check if it contains what you want. You can do this a few ways, but here's just one: if ($('.title-button').html().indexOf('hidden') >= 0) { (For reference, the other methods instead of using indexOf would be .includes and .match; you could also use .test, though that has different syntax since the Regular Expression comes first, so I didn't show it here.)






                share|improve this answer




























                  0














                  Your current code says "set the string .title-button equal to the string hidden and, if that value is truthy (i.e. true, a nonzero number, a nonempty string, etc.), then do something". That makes no sense. So let's take this step-by-step.



                  "My expected result is to be able to check the string in the class."



                  What is "the string in the class"? A class name is a string, which simply indicates a group that an element belongs to. If you're trying to check whether an element which has the class title-button also has the class hidden, then you'd want to first get the element -- not just its class name -- and check all its classes for the class hidden. With jQuery, you can get an element from one of its classes by using $('.class-name'), so in this case, $('.title-button'). Then you can check if it has a certain class using the .hasClass('new-class-name)method. Soif ($('.title-button').hasClass('hidden')) {` is the condition you want to check.



                  If instead you're trying to check if the contents of the element contain the string "hidden", then you need to first get the contents and then check if it contains what you want. You can do this a few ways, but here's just one: if ($('.title-button').html().indexOf('hidden') >= 0) { (For reference, the other methods instead of using indexOf would be .includes and .match; you could also use .test, though that has different syntax since the Regular Expression comes first, so I didn't show it here.)






                  share|improve this answer


























                    0












                    0








                    0







                    Your current code says "set the string .title-button equal to the string hidden and, if that value is truthy (i.e. true, a nonzero number, a nonempty string, etc.), then do something". That makes no sense. So let's take this step-by-step.



                    "My expected result is to be able to check the string in the class."



                    What is "the string in the class"? A class name is a string, which simply indicates a group that an element belongs to. If you're trying to check whether an element which has the class title-button also has the class hidden, then you'd want to first get the element -- not just its class name -- and check all its classes for the class hidden. With jQuery, you can get an element from one of its classes by using $('.class-name'), so in this case, $('.title-button'). Then you can check if it has a certain class using the .hasClass('new-class-name)method. Soif ($('.title-button').hasClass('hidden')) {` is the condition you want to check.



                    If instead you're trying to check if the contents of the element contain the string "hidden", then you need to first get the contents and then check if it contains what you want. You can do this a few ways, but here's just one: if ($('.title-button').html().indexOf('hidden') >= 0) { (For reference, the other methods instead of using indexOf would be .includes and .match; you could also use .test, though that has different syntax since the Regular Expression comes first, so I didn't show it here.)






                    share|improve this answer













                    Your current code says "set the string .title-button equal to the string hidden and, if that value is truthy (i.e. true, a nonzero number, a nonempty string, etc.), then do something". That makes no sense. So let's take this step-by-step.



                    "My expected result is to be able to check the string in the class."



                    What is "the string in the class"? A class name is a string, which simply indicates a group that an element belongs to. If you're trying to check whether an element which has the class title-button also has the class hidden, then you'd want to first get the element -- not just its class name -- and check all its classes for the class hidden. With jQuery, you can get an element from one of its classes by using $('.class-name'), so in this case, $('.title-button'). Then you can check if it has a certain class using the .hasClass('new-class-name)method. Soif ($('.title-button').hasClass('hidden')) {` is the condition you want to check.



                    If instead you're trying to check if the contents of the element contain the string "hidden", then you need to first get the contents and then check if it contains what you want. You can do this a few ways, but here's just one: if ($('.title-button').html().indexOf('hidden') >= 0) { (For reference, the other methods instead of using indexOf would be .includes and .match; you could also use .test, though that has different syntax since the Regular Expression comes first, so I didn't show it here.)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 18:13









                    IceMetalPunkIceMetalPunk

                    989716




                    989716























                        0














                        If you are trying to find the text "hidden" in the div elements content,for example



                         <div class="title-button">I am hidden</div>

                        If(document.querySelector(".title-button").innerHTML.indexOf("hidden") != -1){
                        //Your statement goes here
                        }


                        If you are trying to find that the div element has class "hidden" in its class list,then there are two scenarios,



                        1)If you have multiple classes on the div element.For example,



                        <div class="title-button class2 class3"></div>

                        If(document.querySelector(".title-button").className.indexOf("hidden") != -1){
                        //Your statement goes here
                        }


                        2)If you have only one class as follows,



                        <div class="title-button"></div>

                        If(document.querySelector(".title-button").className == "hidden"){
                        //Your statement goes here
                        }





                        share|improve this answer






























                          0














                          If you are trying to find the text "hidden" in the div elements content,for example



                           <div class="title-button">I am hidden</div>

                          If(document.querySelector(".title-button").innerHTML.indexOf("hidden") != -1){
                          //Your statement goes here
                          }


                          If you are trying to find that the div element has class "hidden" in its class list,then there are two scenarios,



                          1)If you have multiple classes on the div element.For example,



                          <div class="title-button class2 class3"></div>

                          If(document.querySelector(".title-button").className.indexOf("hidden") != -1){
                          //Your statement goes here
                          }


                          2)If you have only one class as follows,



                          <div class="title-button"></div>

                          If(document.querySelector(".title-button").className == "hidden"){
                          //Your statement goes here
                          }





                          share|improve this answer




























                            0












                            0








                            0







                            If you are trying to find the text "hidden" in the div elements content,for example



                             <div class="title-button">I am hidden</div>

                            If(document.querySelector(".title-button").innerHTML.indexOf("hidden") != -1){
                            //Your statement goes here
                            }


                            If you are trying to find that the div element has class "hidden" in its class list,then there are two scenarios,



                            1)If you have multiple classes on the div element.For example,



                            <div class="title-button class2 class3"></div>

                            If(document.querySelector(".title-button").className.indexOf("hidden") != -1){
                            //Your statement goes here
                            }


                            2)If you have only one class as follows,



                            <div class="title-button"></div>

                            If(document.querySelector(".title-button").className == "hidden"){
                            //Your statement goes here
                            }





                            share|improve this answer















                            If you are trying to find the text "hidden" in the div elements content,for example



                             <div class="title-button">I am hidden</div>

                            If(document.querySelector(".title-button").innerHTML.indexOf("hidden") != -1){
                            //Your statement goes here
                            }


                            If you are trying to find that the div element has class "hidden" in its class list,then there are two scenarios,



                            1)If you have multiple classes on the div element.For example,



                            <div class="title-button class2 class3"></div>

                            If(document.querySelector(".title-button").className.indexOf("hidden") != -1){
                            //Your statement goes here
                            }


                            2)If you have only one class as follows,



                            <div class="title-button"></div>

                            If(document.querySelector(".title-button").className == "hidden"){
                            //Your statement goes here
                            }






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 2 at 18:33

























                            answered Jan 2 at 18:25









                            Aravind Kumar JAravind Kumar J

                            11




                            11






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011061%2fhow-to-use-if-on-a-specific-string%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith