Skip to content Skip to sidebar Skip to footer

Template Literal Is Empty When Used Within Single Quotes

I have this line of code that works: var result = '$request.getParameter('${widgetId}_data')';

Solution 1:

You should use backticks for Template Literals.

console.log(`${element.id}_data`);

Solution 2:

Single and double quotes are used for string literals. There's no special meaning of ${...} inside string literals, it's just taken literally.

Use backticks to make a template literal. Sinxw you're trying to create a function call string, put the entire string inside the backticks. Then put single quotes or double quotes around the argument.

You also need # in the first selector to select the sample element.

var element = jQuery("#SomeSampleElement")[0];
var result = `$request.getParameter('${element.id}_data')`;
console.log(result);

It appears that you're using a 3rd-party template engine that uses ${...} for its own purposes. SO you should use concatenation instead:

var result = '$request.getParameter("' + element.id_data + '")';

Post a Comment for "Template Literal Is Empty When Used Within Single Quotes"