1

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?

1
  • You need to give the type of event you want to bind to. You are saying each where you should say click or change etc... Commented Feb 24, 2012 at 9:41

2 Answers 2

1

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()

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

5 Comments

i have a table with many rows . i am using pagination to shows 10 entries per page. this function working for the data present in first page. is there any work around to make this function to work for all the pages even if i am seaching it from first page.
If only the 10 entries are present in your page (that is - in your markup) then no. Is the page being generated on the server from a larger set of data?
on loading the page all data will be generated. but using pagination (jquery plugin) i am showing only 10 entries. on clicking next button it will show next 10 entries.
Sorry, I'm not sure how the pagination plugin works. However if it is just hiding table rows then you should be able to do this - but you may end up confusing the pagination plugin.
Basically pagination hides table rows.
1

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();
        }
    });
});

4 Comments

+1 for delegate - didn't know about that.. And an imaginary +another for understanding what the OP was actually doing :D
i have a table with many rows . i am using pagination to shows 10 entries per page. this function working for the data present in first page. is there any work around to make this function to work for all the pages even if i am seaching it from first page.
@ElRonnoco you might also want to know about on for jquery 1.7+ api.jquery.com/on
@optimusprime619 i think that's the biggest jQuery API post I've ever seen! I'll plough through that at my leisure :)

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.