1

I have the following:

.find("[data-list='read']")

and I want to add a JS variable inside, something like this:

.find("[data-list=''+ $variable +'']")

Obviously, the two single quotations won't work in this case. Do I need to escape the single quotations in some way? What's the approach in JS?

Henrik Petterson
  • 6,776
  • 19
  • 65
  • 144

3 Answers3

1

You can use template literals or just concatenate the strings preserving the "text'" + var + "'text" style.

var variable = 'myText';
var tlSelector = `[data-list='${variable}']`;
var concStrings = "[data-list='" + variable +"']";

console.log(tlSelector);
console.log(concStrings);
Suren Srapyan
  • 62,337
  • 13
  • 111
  • 105
0

String concatenation should look like this:

"[data-list='" + $variable + "']"

To break it down: string1 + string2 + string3 where

string1 = "[data-list='"
string2 = $variable
string3 = "']"
dfsq
  • 187,712
  • 24
  • 229
  • 250
0

You just need to concatenate variable like this:

.find("[data-list='" + read + "']")
  • Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". – abarisone Nov 16 '17 at 13:52