Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reapply a jQuery selector?

Tags:

jquery

I have a javascript function that selects several elements, and then performs some DOM manipulation. When the DOM manipulation involves adding more elements that match the initial selectors, I'd like to re-apply them to include the new elements.

var row1 = table.find("tr.row1 td");
var row2 = table.find("tr.row2 td");
var row3 = table.find("tr.row3 td");

for(...) {
   if() {//Some condition is met
       table.children().each(function(row) {
          row.append(row.children().eq(5).clone());
       });
       row1.refresh(); // I'd like something here like refresh, rather than row1 = table.find("tr.row1 td");
    }
    //Code that uses row1, row2, and row3
}

Is there a function that does what I'm looking for on the refresh() line?

like image 495
Chris Marasti-Georg Avatar asked Dec 22 '22 04:12

Chris Marasti-Georg


1 Answers

There isn't really a function that does what you've described, you just re-issue the original request.

If you have a jQuery object (like your row1), you can use this undocumented mechanism to re-run the original request:

row1 = $(row1.selector, row1.context);

...but again, the selector property is undocumented (context is documented) and so while it works now in 1.7.1, it may well not work in 1.7.2.

A better approach may be to only keep hold of the jQuery object for the rows as long as you're actually doing something with it, but it depends on the cost of re-acquiring the list and how frequently you do that.

like image 141
T.J. Crowder Avatar answered Jan 04 '23 22:01

T.J. Crowder