3

I have a click event listener as below:

$(".clickable").click(function (e) {

  var results= $(e.currentTarget).closest('div.set').find('.clickable');
      // returns 
      //[<label for=​"thumbnail" class=​"clickable">​1 Malaysia​</label>​,
      //<div class=​"thumbnail clickable">​…​</div>​]

  var label = $(results).find('label'); // returns [] (empty list)

}

My problem is, how can I select the label element from the results list ? Thanks !

1
  • 1
    Also, in all likelihood e.currentTarget should be this. Commented Jun 24, 2014 at 10:42

1 Answer 1

2

Try to use .filter() instead of .find(),

var label = results.filter('label');

.find() will search for descendants, but here we are in need to filter out the required element from the collection, so use .filter(selector) here.


And also you could have simply used as satpal said like,

var results= $(e.currentTarget).closest('div.set').find('label.clickable');
Sign up to request clarification or add additional context in comments.

2 Comments

@Unicorn, Just curious Why not var results= $(e.currentTarget).closest('div.set').find('label.clickable');?
@Satpal, cause I will be using the values in Div element later, simply personal preferences that I like to select one time for the general elements (results) then further filter in the children, instead of doing 2 almost-alike selection. Eg: $(e.currentTarget).closest('div.set').find('label.clickable') and $(e.currentTarget).closest('div.set').find('div.clickable')

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.