Why aren't my classes being added and removed as expected?











up vote
0
down vote

favorite












I'm trying to use J Query addClass and RemoveClass to set my selected tab class attribute with the active class. I added alert to make sure the function fires, and the alert message shows up.
problem is addClass and removeClass don't seem to be working.



        $(document).ready(function () {
$('a').click(function () {
alert(this.tagName);
$('a').removeClass("active");
$(this).addClass("active");

});
});


I'm using asp.net MVC for building the links, and bootstrap for styling:



        <ul class="nav navbar-nav text-center">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Recommendations", "Recommendations", "Home")</li>
<li>@Html.ActionLink("Gallery", "Gallery", "Home")</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">actions
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">1</a></li>
<li><a class="dropdown-item" href="#">2</a></li>
<li><a class="dropdown-item" href="#">3</a></li>
</ul>
</li>
</ul>


this is the HTML code as copied from browser after clicking the link and getting alert message, but none of the a tags has the active class...



    <ul class="nav navbar-nav text-center">
<li><a href="/Home/Index">Home</a></li>
<li><a href="/">About</a></li>
<li><a href="/Home/Recommendations">Recommendations</a></li>
<li><a href="/Home/Gallery">Gallery</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">auctions
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">1</a></li>
<li><a class="dropdown-item" href="#">2</a></li>
<li><a class="dropdown-item" href="#">3</a></li>
</ul>
</li>
</ul>









share|improve this question
























  • Have you verified in the browser developer tools that the values you are setting aren't being overwritten by other classes associated with the HTML elements?
    – cminus
    Nov 19 at 17:55










  • Also, you should restrict the selector on click and when you remove the class. As it is, you're hitting every anchor in the page. $('.dropdown-menu a')
    – isherwood
    Nov 19 at 17:56












  • I already tried your code and it is working on my end. Make sure your script is being imported correctly.
    – Alain Cruz
    Nov 19 at 18:01






  • 3




    I think that is the parent li that need that active
    – Roy
    Nov 19 at 18:01










  • What are you seeing that makes you think it is not working?
    – Taplar
    Nov 19 at 18:01















up vote
0
down vote

favorite












I'm trying to use J Query addClass and RemoveClass to set my selected tab class attribute with the active class. I added alert to make sure the function fires, and the alert message shows up.
problem is addClass and removeClass don't seem to be working.



        $(document).ready(function () {
$('a').click(function () {
alert(this.tagName);
$('a').removeClass("active");
$(this).addClass("active");

});
});


I'm using asp.net MVC for building the links, and bootstrap for styling:



        <ul class="nav navbar-nav text-center">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Recommendations", "Recommendations", "Home")</li>
<li>@Html.ActionLink("Gallery", "Gallery", "Home")</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">actions
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">1</a></li>
<li><a class="dropdown-item" href="#">2</a></li>
<li><a class="dropdown-item" href="#">3</a></li>
</ul>
</li>
</ul>


this is the HTML code as copied from browser after clicking the link and getting alert message, but none of the a tags has the active class...



    <ul class="nav navbar-nav text-center">
<li><a href="/Home/Index">Home</a></li>
<li><a href="/">About</a></li>
<li><a href="/Home/Recommendations">Recommendations</a></li>
<li><a href="/Home/Gallery">Gallery</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">auctions
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">1</a></li>
<li><a class="dropdown-item" href="#">2</a></li>
<li><a class="dropdown-item" href="#">3</a></li>
</ul>
</li>
</ul>









share|improve this question
























  • Have you verified in the browser developer tools that the values you are setting aren't being overwritten by other classes associated with the HTML elements?
    – cminus
    Nov 19 at 17:55










  • Also, you should restrict the selector on click and when you remove the class. As it is, you're hitting every anchor in the page. $('.dropdown-menu a')
    – isherwood
    Nov 19 at 17:56












  • I already tried your code and it is working on my end. Make sure your script is being imported correctly.
    – Alain Cruz
    Nov 19 at 18:01






  • 3




    I think that is the parent li that need that active
    – Roy
    Nov 19 at 18:01










  • What are you seeing that makes you think it is not working?
    – Taplar
    Nov 19 at 18:01













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to use J Query addClass and RemoveClass to set my selected tab class attribute with the active class. I added alert to make sure the function fires, and the alert message shows up.
problem is addClass and removeClass don't seem to be working.



        $(document).ready(function () {
$('a').click(function () {
alert(this.tagName);
$('a').removeClass("active");
$(this).addClass("active");

});
});


I'm using asp.net MVC for building the links, and bootstrap for styling:



        <ul class="nav navbar-nav text-center">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Recommendations", "Recommendations", "Home")</li>
<li>@Html.ActionLink("Gallery", "Gallery", "Home")</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">actions
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">1</a></li>
<li><a class="dropdown-item" href="#">2</a></li>
<li><a class="dropdown-item" href="#">3</a></li>
</ul>
</li>
</ul>


this is the HTML code as copied from browser after clicking the link and getting alert message, but none of the a tags has the active class...



    <ul class="nav navbar-nav text-center">
<li><a href="/Home/Index">Home</a></li>
<li><a href="/">About</a></li>
<li><a href="/Home/Recommendations">Recommendations</a></li>
<li><a href="/Home/Gallery">Gallery</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">auctions
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">1</a></li>
<li><a class="dropdown-item" href="#">2</a></li>
<li><a class="dropdown-item" href="#">3</a></li>
</ul>
</li>
</ul>









share|improve this question















I'm trying to use J Query addClass and RemoveClass to set my selected tab class attribute with the active class. I added alert to make sure the function fires, and the alert message shows up.
problem is addClass and removeClass don't seem to be working.



        $(document).ready(function () {
$('a').click(function () {
alert(this.tagName);
$('a').removeClass("active");
$(this).addClass("active");

});
});


I'm using asp.net MVC for building the links, and bootstrap for styling:



        <ul class="nav navbar-nav text-center">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Recommendations", "Recommendations", "Home")</li>
<li>@Html.ActionLink("Gallery", "Gallery", "Home")</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">actions
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">1</a></li>
<li><a class="dropdown-item" href="#">2</a></li>
<li><a class="dropdown-item" href="#">3</a></li>
</ul>
</li>
</ul>


this is the HTML code as copied from browser after clicking the link and getting alert message, but none of the a tags has the active class...



    <ul class="nav navbar-nav text-center">
<li><a href="/Home/Index">Home</a></li>
<li><a href="/">About</a></li>
<li><a href="/Home/Recommendations">Recommendations</a></li>
<li><a href="/Home/Gallery">Gallery</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">auctions
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">1</a></li>
<li><a class="dropdown-item" href="#">2</a></li>
<li><a class="dropdown-item" href="#">3</a></li>
</ul>
</li>
</ul>






jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 18:07

























asked Nov 19 at 17:49









c.d.

127




127












  • Have you verified in the browser developer tools that the values you are setting aren't being overwritten by other classes associated with the HTML elements?
    – cminus
    Nov 19 at 17:55










  • Also, you should restrict the selector on click and when you remove the class. As it is, you're hitting every anchor in the page. $('.dropdown-menu a')
    – isherwood
    Nov 19 at 17:56












  • I already tried your code and it is working on my end. Make sure your script is being imported correctly.
    – Alain Cruz
    Nov 19 at 18:01






  • 3




    I think that is the parent li that need that active
    – Roy
    Nov 19 at 18:01










  • What are you seeing that makes you think it is not working?
    – Taplar
    Nov 19 at 18:01


















  • Have you verified in the browser developer tools that the values you are setting aren't being overwritten by other classes associated with the HTML elements?
    – cminus
    Nov 19 at 17:55










  • Also, you should restrict the selector on click and when you remove the class. As it is, you're hitting every anchor in the page. $('.dropdown-menu a')
    – isherwood
    Nov 19 at 17:56












  • I already tried your code and it is working on my end. Make sure your script is being imported correctly.
    – Alain Cruz
    Nov 19 at 18:01






  • 3




    I think that is the parent li that need that active
    – Roy
    Nov 19 at 18:01










  • What are you seeing that makes you think it is not working?
    – Taplar
    Nov 19 at 18:01
















Have you verified in the browser developer tools that the values you are setting aren't being overwritten by other classes associated with the HTML elements?
– cminus
Nov 19 at 17:55




Have you verified in the browser developer tools that the values you are setting aren't being overwritten by other classes associated with the HTML elements?
– cminus
Nov 19 at 17:55












Also, you should restrict the selector on click and when you remove the class. As it is, you're hitting every anchor in the page. $('.dropdown-menu a')
– isherwood
Nov 19 at 17:56






Also, you should restrict the selector on click and when you remove the class. As it is, you're hitting every anchor in the page. $('.dropdown-menu a')
– isherwood
Nov 19 at 17:56














I already tried your code and it is working on my end. Make sure your script is being imported correctly.
– Alain Cruz
Nov 19 at 18:01




I already tried your code and it is working on my end. Make sure your script is being imported correctly.
– Alain Cruz
Nov 19 at 18:01




3




3




I think that is the parent li that need that active
– Roy
Nov 19 at 18:01




I think that is the parent li that need that active
– Roy
Nov 19 at 18:01












What are you seeing that makes you think it is not working?
– Taplar
Nov 19 at 18:01




What are you seeing that makes you think it is not working?
– Taplar
Nov 19 at 18:01












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










you can do like



<li class="@(ViewContext.RouteData.Values["Action"].ToString() == "Index" ? "active" : "")">@Html.ActionLink("Home", "Index", "Home")</li>





share|improve this answer




























    up vote
    0
    down vote













    ok, found the answer based on what said @DnyanWaychal that page is redirected to another page after adding the class, so i changed if to influence the right page:



    $(function() {
    var href = window.location.href;
    $('a').each(function(e,i) {
    if (href.indexOf($(this).attr('href')) >= 0) {
    $(this).addClass('active');
    }
    });
    });





    share|improve this answer




























      up vote
      -1
      down vote













      You can have a separate class for the tabs. In that case please try the below code.



      $(document).ready(function () {
      $('<className> a').click(function () {
      $(this).toggleClass("active");

      });
      });





      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',
        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%2f53380115%2fwhy-arent-my-classes-being-added-and-removed-as-expected%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








        up vote
        0
        down vote



        accepted










        you can do like



        <li class="@(ViewContext.RouteData.Values["Action"].ToString() == "Index" ? "active" : "")">@Html.ActionLink("Home", "Index", "Home")</li>





        share|improve this answer

























          up vote
          0
          down vote



          accepted










          you can do like



          <li class="@(ViewContext.RouteData.Values["Action"].ToString() == "Index" ? "active" : "")">@Html.ActionLink("Home", "Index", "Home")</li>





          share|improve this answer























            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            you can do like



            <li class="@(ViewContext.RouteData.Values["Action"].ToString() == "Index" ? "active" : "")">@Html.ActionLink("Home", "Index", "Home")</li>





            share|improve this answer












            you can do like



            <li class="@(ViewContext.RouteData.Values["Action"].ToString() == "Index" ? "active" : "")">@Html.ActionLink("Home", "Index", "Home")</li>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 at 18:29









            Dnyan Waychal

            1,2971024




            1,2971024
























                up vote
                0
                down vote













                ok, found the answer based on what said @DnyanWaychal that page is redirected to another page after adding the class, so i changed if to influence the right page:



                $(function() {
                var href = window.location.href;
                $('a').each(function(e,i) {
                if (href.indexOf($(this).attr('href')) >= 0) {
                $(this).addClass('active');
                }
                });
                });





                share|improve this answer

























                  up vote
                  0
                  down vote













                  ok, found the answer based on what said @DnyanWaychal that page is redirected to another page after adding the class, so i changed if to influence the right page:



                  $(function() {
                  var href = window.location.href;
                  $('a').each(function(e,i) {
                  if (href.indexOf($(this).attr('href')) >= 0) {
                  $(this).addClass('active');
                  }
                  });
                  });





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    ok, found the answer based on what said @DnyanWaychal that page is redirected to another page after adding the class, so i changed if to influence the right page:



                    $(function() {
                    var href = window.location.href;
                    $('a').each(function(e,i) {
                    if (href.indexOf($(this).attr('href')) >= 0) {
                    $(this).addClass('active');
                    }
                    });
                    });





                    share|improve this answer












                    ok, found the answer based on what said @DnyanWaychal that page is redirected to another page after adding the class, so i changed if to influence the right page:



                    $(function() {
                    var href = window.location.href;
                    $('a').each(function(e,i) {
                    if (href.indexOf($(this).attr('href')) >= 0) {
                    $(this).addClass('active');
                    }
                    });
                    });






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 at 18:27









                    c.d.

                    127




                    127






















                        up vote
                        -1
                        down vote













                        You can have a separate class for the tabs. In that case please try the below code.



                        $(document).ready(function () {
                        $('<className> a').click(function () {
                        $(this).toggleClass("active");

                        });
                        });





                        share|improve this answer



























                          up vote
                          -1
                          down vote













                          You can have a separate class for the tabs. In that case please try the below code.



                          $(document).ready(function () {
                          $('<className> a').click(function () {
                          $(this).toggleClass("active");

                          });
                          });





                          share|improve this answer

























                            up vote
                            -1
                            down vote










                            up vote
                            -1
                            down vote









                            You can have a separate class for the tabs. In that case please try the below code.



                            $(document).ready(function () {
                            $('<className> a').click(function () {
                            $(this).toggleClass("active");

                            });
                            });





                            share|improve this answer














                            You can have a separate class for the tabs. In that case please try the below code.



                            $(document).ready(function () {
                            $('<className> a').click(function () {
                            $(this).toggleClass("active");

                            });
                            });






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 19 at 18:06

























                            answered Nov 19 at 18:01









                            Kapil gopinath

                            1,0731615




                            1,0731615






























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f53380115%2fwhy-arent-my-classes-being-added-and-removed-as-expected%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