temp ali
temp ali

Reputation: 11

find element that has certain class and then select it

the element can change according to users' action, that's why I use on()

$(document).ready(function(){

$(document).on('click','li', function(){
  if($('li').hasClass()){

    //do something <-- but it return true (boolean)
  }

});

Upvotes: 1

Views: 40

Answers (2)

Bibhu
Bibhu

Reputation: 4081

Use this :

$(document).ready(function(){

$(document).on('click','li', function(){
  if(if ($('li').hasClass('yourclassname'))){

    //do something <-- but it return true (boolean)
  }
});

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382434

You probably want

$(document).on('click','li.yourclass', function(){

    //do something with your element, which is "this"

});

The 'li.yourclass' selector is dynamically tested at each click, so you don't need to test in the callback.

Upvotes: 2

Related Questions