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>
jquery
|
show 10 more comments
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>
jquery
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 parentlithat need thatactive
– Roy
Nov 19 at 18:01
What are you seeing that makes you think it is not working?
– Taplar
Nov 19 at 18:01
|
show 10 more comments
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>
jquery
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
jquery
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 parentlithat need thatactive
– Roy
Nov 19 at 18:01
What are you seeing that makes you think it is not working?
– Taplar
Nov 19 at 18:01
|
show 10 more comments
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 parentlithat need thatactive
– 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
|
show 10 more comments
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>
add a comment |
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');
}
});
});
add a comment |
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");
});
});
add a comment |
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>
add a comment |
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>
add a comment |
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>
you can do like
<li class="@(ViewContext.RouteData.Values["Action"].ToString() == "Index" ? "active" : "")">@Html.ActionLink("Home", "Index", "Home")</li>
answered Nov 19 at 18:29
Dnyan Waychal
1,2971024
1,2971024
add a comment |
add a comment |
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');
}
});
});
add a comment |
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');
}
});
});
add a comment |
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');
}
});
});
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');
}
});
});
answered Nov 19 at 18:27
c.d.
127
127
add a comment |
add a comment |
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");
});
});
add a comment |
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");
});
});
add a comment |
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");
});
});
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");
});
});
edited Nov 19 at 18:06
answered Nov 19 at 18:01
Kapil gopinath
1,0731615
1,0731615
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
lithat need thatactive– Roy
Nov 19 at 18:01
What are you seeing that makes you think it is not working?
– Taplar
Nov 19 at 18:01