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');
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
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.
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>