maaz
maaz

Reputation: 3664

Jquery-Binding each function with Live event does not work

I have function

$('table tr:gt(0)').each(function() {
if ($('td:contains("'+ pointName +'")', this).length == 0) $(this).hide();
});

i want this function to be bind with live().

i have tried something like this which couldn't work.

 $('table tr:gt(0)').live('each', function (){
if ($('td:contains("'+ pointName +'")', this).length == 0) $(this).hide();
 });

what is the correct way?

Upvotes: 1

Views: 2493

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337627

each() is a method of iterating over a set of elements. live() is for binding to events. Therefore because each() is not an event, you can't use it as you have with live(). Instead you should bind to an event, like click or hover, and then use the each() code you have in your first example within the handler.

Finally, live() is deprecated. If you are using an older version of jQuery you should use delegate() instead.

Try this:

$("#myContainer").delegate('#myButton', 'click', function() {
    $('table tr:gt(0)').each(function() {
        if ($('td:contains("'+ pointName +'")', this).length == 0) {
            $(this).hide();
        }
    });
});

Upvotes: 1

El Ronnoco
El Ronnoco

Reputation: 11912

You don't need to say each - in fact this is incorrect usage here. The first argument to live specifies the event type. change, keyup etc etc...

live - like most jQuery methods acts on the whole selected set. So you probably want...

$('table tr:gt(0)').live('click', function (){
    if ($('td:contains("'+ pointName +'")', this).length == 0) $(this).hide();
});

live can also take multiple events - for example .live('change click keyup', function()... will bind to all three events.

jQuery.live()

Upvotes: 1

Related Questions