0

I want add a class when a <li> element clicked. but it didn't work. Here's JQuery snippet

$(".mama").click(function(){        
    var arr = ["cat","dog", "mice", "bird"];
    alink =  $(this).text();        

    $.each(arr, function(index, value) {
        if(alink == value){
            $(this).addClass('hi'); 
        }
    }); 
});

And HTML

<ul id="animal">
    <li class="mama"><a href="#">cat</a></li>
    <li class="mama"><a href="#">dog</a></li>
    <li class="mama"><a href="#">Mice</a></li>
</ul>

i also have tried to do it by .map but nothing happen. please give solution and some explain. Thanks

3
  • 1
    Downvotes for everyone that encourages this poster to use implicitly declared and/or global variables and to define an array inside a function for no reason. Commented Sep 11, 2013 at 6:47
  • Rather than give suggestions on correcting this, we should be pointing out that using a click event to find the text of clicked element, and then looping over all possible text elements in order to confirm that it should be triggered is the wrong way to do it. Commented Sep 11, 2013 at 6:49
  • @m59, I'm assuming this is a reduced test case, and the array serves a purpose in the real code. Good call about the global var though. Commented Sep 11, 2013 at 6:49

6 Answers 6

2

This should work:

$(".mama").click(function(){   
    var $this = $(this);     
    var arr = ["cat","dog", "mice", "bird"];
    alink =  $(this).text();        

    $.each(arr, function(index, value) {
        if(alink == value){
            $this.addClass('hi'); 
        }
    }); 
});

because this inside the each is not the DOM element <li>.

Sign up to request clarification or add additional context in comments.

Comments

1

You probably just need to do this:

http://jsfiddle.net/reugB/1/

$('.mama').on('click', function(evt) {
    evt.preventDefault();
    $(this).addClass('hi');
});

Comments

0

Use this to refer to current element which was clicked.

$(".mama").click(function (e) {
    e.preventDefault();
    $(this).addClass('hi');
});

Comments

0

If you mean add the class hi to the clicked li, just do like below:

$(".mama").click(function(e){
    e.preventDefault();
    $(".mama").removeClass('hi');
    $(this).addClass('hi')        
});

1 Comment

@Dogbert Yep, you are right. Removed that. But I don't think the op wants to add class to all the lis.
0
 .hi { background-color: #f00 !important;}


 $('.mama').on('click', function(evt) {
evt.preventDefault();
$(this).addClass('hi');

});

1 Comment

make .hi class color important
-1
$(".mama").click(function(e){    
e.preventDefault();    
    var arr = ["cat","dog", "mice", "bird"];
    var alink =  $(this).find("a").text();        

    $.each(arr, function(index, value) {
        if(alink == value){
            $(this).addClass('hi'); 
        }
    }); 
});

see reference text and find

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.