0

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

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

@MShack You need to invoke those functions to return the results. $('.two_column_layout').find(getPlayerByeQuery() + ',' + getPlayerQuery()).css('background', 'red'); - Works just fine.
ty , forgot the () aside each one
1

same as concatenating strings

$('table').find(varONE + ',' + varTWO).css('background', 'red');

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.