0

So, if I have a table with the class name site-table. In one of the table cells, there is a text 'Appliance' , then I can do this:

$(".site-table")
(".site-table:contains('Appliance')");

Now, say I have a variable called the_table:

$the_table = '.site-table'

when I replace the ".site-table" with $the_table variable, I get errors:

$("$element:contains('Appliance')");

jquery-3.1.1.slim.min.js:2 Uncaught Error: Syntax error, unrecognized expression: $element:contains('Appliance')
at Function.ga.error (jquery-3.1.1.slim.min.js:2)
at ga.tokenize (jquery-3.1.1.slim.min.js:2)
at ga.select (jquery-3.1.1.slim.min.js:2)
at Function.ga [as find] (jquery-3.1.1.slim.min.js:2)
at r.fn.init.find (jquery-3.1.1.slim.min.js:2)
at new r.fn.init (jquery-3.1.1.slim.min.js:2)
at r (jquery-3.1.1.slim.min.js:2)
at <anonymous>:1:1

So, then the question is how to pass filters to variables in jQuery. This is particularly useful when using a generic function rather than coding for each individual repetitive case.

EarlyCoder
  • 1,213
  • 2
  • 18
  • 42
  • You didn't show us what the code that uses your `the_table`variable looks like. – peeebeee Dec 05 '18 at 15:44
  • 4
    You need to concatenate like `$($element+":contains('Appliance')");` – Kubwimana Adrien Dec 05 '18 at 15:46
  • Or use template literals. Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals – Taplar Dec 05 '18 at 15:50
  • Kubwimana's answer is correct. I found that out right after I posted the question. I went to grab a cup of coffee, and all these answers in a few minutes! Thank you all – EarlyCoder Dec 05 '18 at 15:54

0 Answers0