dukevin
dukevin

Reputation: 23188

Jquery access functions from an $.each loop

I have a list of checkboxes and want to do something with all the names of the checkboxes. But I can't seem to access the HTML objects:

$('.update').click(function(){
                $('input[type=checkbox]:checked').each(function(i,elem){
                    console.log(elem);
                    elem.hide();
                });
            });

This produces TypeError: elem.hide is not a function

But the console.log(elem) shows: <input type="checkbox" name="TV">

How can I access each element?

Upvotes: 0

Views: 63

Answers (2)

Felix
Felix

Reputation: 10078

elem is DOM element, not jquery type. You can use $(elem).hide() instead

Upvotes: 1

rioc0719
rioc0719

Reputation: 303

You are accessing the DOM node directly by using elem. You need to pass it to $() to get a jQuery object with access to .hide() and other jQuery methods:

$('.update').click(function() {
  $('input[type=checkbox]:checked').each(function(i, elem) {
    $(elem).hide();
  });
});

Upvotes: 3

Related Questions