Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a variable into :contains selector

window.something.updateStatus = function(theName) {
  $('#myTable').children('tr').remove(":contains('theName')");
};

Obviously the above does not work because it is looking for a string called "theName" in any in myTable.

What I'd like to do is pass in theName's Value to the contains.

How do I evaluate this expression?

Thanks.

like image 839
Shawn J. Molloy Avatar asked Jul 12 '11 22:07

Shawn J. Molloy


People also ask

Can I put variable in jQuery selector?

Yes, it is possible to pass a variable into a jQuery attribute-contains selector. The [attribute*=value] selector is used to select each element with a specific attribute and a value containing a string.

How to pass variable in jQuery function?

It's jQuery equivalent would be something like this: $('button'). on('click', myFunction); Passing an argument to that function would then be something like this: $('button'). on('click', myFunction('test')); .

How use contains in jQuery?

The :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).


1 Answers

This is untested, but should work:

window.something.updateStatus = function(theName) {
  $('#myTable').children('tr').remove(":contains('" + theName +"')");
};

Essentially it removes the variable theName from the string, but still quotes that value (once the variable's interpolated), which is why there's an opening, and closing, ' either side of the variable and + concatenation operators.

like image 68
David Thomas Avatar answered Oct 17 '22 02:10

David Thomas