Add / remove class by range inside tag

I tried various options, but can't get the dumb job to work. How to get the interval inside <a> in <li> change the class to "active"; then delete it when another <a> is pressed?

<ul id="dumb">
<li><a href="#">Something<span></span></a></li>
<li><a href="#">Something Else<span></span></a></li>
</ul>

Pressing <a> should give the span a class of "active", and when clicked by another, it should remove it from the original and add it to the range of this <a> ...

Thanks!

+5
source share
3 answers
$(function(){
      $("#dumb > li > a").click ( function(){
      $("#dumb > li > a > span").removeClass ( 'active' );
      $(this).find('span').addClass('active');
      return false;
     });
});
+10
source

Try the following:

$(document).ready(function(){
    var $MySpans = $("#dumb li a span");
    $MySpans.click(function(){
        $MySpans.removeClass();
        $(this).addClass("active");
    });
});

You can try this, as it will be a faster selector if it works:

var $MySpans = $("#dumb>li>a>span");
+5
source

jQuery ('# dumb li a span'). addClass ( "active" );

+1

All Articles