1

I need to remove the selected class of an <a> and assign it the last <a> instead. Both are nested within individual <li> elements.

Here's an example of the code:

<ul class="tabs clearfix">
   <li class="tab"><a href="#one" class="selected">Home</a></li>
   <li class="tab"><a href="#two">About</a></li>
   <li class="tab"><a href="#three">Profile</a></li>
   <li class="tab"><a href="#four">History</a></li>
   <li class="tab"><a href="#five">The Beginning of V-Cube</a></li>
</ul>

How can I achieve this using JavaScript/jQuery? Please advise.

EDIT:

Let's say I don't want to target the last tab specifically. Can the href be used as a selector instead?

EDIT #2:

Thank you so much everyone for the quick response. All your answers were spot on, wish I could mark them all as answers :)

3
  • At what point in time do you want this reattribution to occur? Any event's in particular like click/mousedown? Commented Oct 20, 2015 at 9:23
  • of course yes a[href="VALUEOFYOURHREF"] Commented Oct 20, 2015 at 9:28
  • This should occur during the page load event. The idea is to show a different active tab on first load. Commented Oct 20, 2015 at 9:29

5 Answers 5

2

To remove class from an element, use removeClass.

To get the last element, use :last selector or last(). To add new class to element use addClass

$('.selected').removeClass('selected');

$('.tabs li:last a').addClass('selected');
// OR
// $('.tabs li').last().children('a').addClass('selected');
.selected {
  color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="tabs clearfix">
  <li class="tab"><a href="#one" class="selected">Home</a>
  </li>
  <li class="tab"><a href="#two">About</a>
  </li>
  <li class="tab"><a href="#three">Profile</a>
  </li>
  <li class="tab"><a href="#four">History</a>
  </li>
  <li class="tab"><a href="#five">The Beginning of V-Cube</a>
  </li>
</ul>


Update

Let's say I don't want to target the last tab specifically. Can the href be used as a selector instead?

$('.tabs a[href="#five"]').addClass('selected');
Sign up to request clarification or add additional context in comments.

Comments

1

Try this.

$(".tabs li").first().find("a").removeClass("selected");
$(".tabs li").last().find("a").addClass("selected");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="tabs clearfix">
   <li class="tab"><a href="#one" class="selected">Home</a></li>
   <li class="tab"><a href="#two">About</a></li>
   <li class="tab"><a href="#three">Profile</a></li>
   <li class="tab"><a href="#four">History</a></li>
   <li class="tab"><a href="#five">The Beginning of V-Cube</a></li>
</ul>

Comments

1

Try this:

$(document).ready(function(){
   var classname = $(".tabs li:first-child a").attr("class");
   console.log(classname);
   $(".tabs li:last-child a").addClass(classname);
   $(".tabs li:first-child a").removeClass();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<ul class="tabs clearfix">
   <li class="tab"><a href="#one" class="selected">Home</a></li>
   <li class="tab"><a href="#two">About</a></li>
   <li class="tab"><a href="#three">Profile</a></li>
   <li class="tab"><a href="#four">History</a></li>
   <li class="tab"><a href="#five">The Beginning of V-Cube</a></li>
</ul>

Comments

1

it is quite easy also in vanilla JS:

document.querySelector('.selected').classList.remove('selected');

document.querySelector('.tabs li:last a').classList.add('selected');

If you want to use an arbitrary a and select the attribute href then you should use this selector:

a[href="HREFVALUE"]

Comments

1

$('.clearfix').find('.selected').removeClass('selected');
$('.clearfix').find('li:last').find('a').addClass('selected');
$("a[href$='five']").addClass('bold');
.selected {
  color: red
}
.bold {
  font-weight: bold
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="tabs clearfix">
  <li class="tab"><a href="#one" class="selected">Home</a>
  </li>
  <li class="tab"><a href="#two">About</a>
  </li>
  <li class="tab"><a href="#three">Profile</a>
  </li>
  <li class="tab"><a href="#four">History</a>
  </li>
  <li class="tab"><a href="#five">The Beginning of V-Cube</a>
  </li>
</ul>

Just use .removeClass() and .addClass()

.removeClass()

Remove a single class, multiple classes, or all classes from each element in the set of matched elements.

.addClass()

Description: Adds the specified class(es) to each element in the set of matched elements.

2 Comments

@Tushar which is better as selector $('.clearfix').find('li:last').find('a') or $('.clearfix').find('li a:last') I will update the answer if necessary
I think $('.clearfix').find('li:last').find('a'), the reason is that you'll narrow down the search for a by selecting a single li

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.