Reputation: 652
What is the proper way to combine multiple variables
I tried this but didn't work.
varONE = "td.class1";
varTWO = "td.class2";
$('table').find(varONE,varTWO).css('background', 'red');
Upvotes: 0
Views: 49
Reputation: 26355
As others have stated, the .find
method doesn't take multiple selector strings.
If you want to use .find
in that way, you could create your own method.
$.fn.argfind = function () {
return this.find(Array.prototype.join.call(arguments));
};
var one = "td.class1",
two = "td.class2";
$('table').argfind(one, two).css('background', 'red');
<table>
<tr>
<td class="class1">Example</td>
<td>Text</td>
</tr>
<tr>
<td>Example</td>
<td class="class2">Text</td>
</tr>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Upvotes: 1
Reputation: 30557
You need the comma in the string itself like
$('table').find('td.class1,td.class2').css('background', 'red');
or
varONE = "td.class1";
varTWO = "td.class2";
$('table').find(varONE+','+varTWO).css('background', 'red');
The only overloads for find() are a selector
(string) or an element. However, there isn't an overload which accepts multiple selectors
(stings) as separate parameters
Upvotes: 3
Reputation: 3003
Well, you actually need to set the selectors the jquery way, for example
varONE = $("td.class1");
varTWO = $("td.class2");
$('table').find(varONE,varTWO).css('background', 'red');
Then your script will work.
You can also use a concatenated string
varONE = "td.class1";
varTWO = "td.class2";
$('table').find(varONE + "," + varTWO).css('background', 'red');
And it will also work. I recommend you the first solution as it is cleaner, but you can choose.
Upvotes: 2
Reputation: 8961
same as concatenating strings
$('table').find(varONE + ',' + varTWO).css('background', 'red');
Upvotes: 1