javascript - string interpolation for dynamic html attributes - Stack Overflow

Is it possible to use string interpolation for the below (Note the dynamic attributes)document.body.inn

Is it possible to use string interpolation for the below (Note the dynamic attributes)

document.body.innerHTML += <form id="digSigForm" action="${myObj.Url}" method="post"><input type="hidden" name="data" value="${myObj.someVal}"></form>

Is it possible to use string interpolation for the below (Note the dynamic attributes)

document.body.innerHTML += <form id="digSigForm" action="${myObj.Url}" method="post"><input type="hidden" name="data" value="${myObj.someVal}"></form>
Share Improve this question edited Jan 10, 2017 at 8:40 Suren Srapyan 68.7k14 gold badges125 silver badges117 bronze badges asked Jan 10, 2017 at 8:33 copenndthagencopenndthagen 50.8k105 gold badges313 silver badges490 bronze badges 1
  • Do you expect the attributes to be html-escaped? Did you mean to use a template literal and just forgot the backticks? It's not quite clear what the question/problem is here. – Bergi Commented Jul 21, 2024 at 19:14
Add a ment  | 

3 Answers 3

Reset to default 0

You should use the backticks to define a string with string interpollation: ``

Like this:

console.log(`1 and 1 make ${1 + 1}`);

This is from the typescript documentation :

Another mon use case is when you want to generate some string out of some static strings + some variables. For this you would need some templating logic and this is where template strings get their name from. Here's how you would potentially generate an html string previously:

var lyrics = 'Never gonna give you up';
var html = '<div>' + lyrics + '</div>';

Now with template strings you can just do:

var lyrics = 'Never gonna give you up';
var html = `<div>${lyrics}</div>`;

Note that any placeholder inside the interpolation (${ and }) is treated as a JavaScript expression and evaluated as such e.g. you can do fancy math.

console.log(`1 and 1 make ${1 + 1}`);
document.body.innerHTML += `<form id="digSigForm" action="${myObj.Url}" method="post"><input type="hidden" name="data" value="${myObj.someVal}"></form>`;

You forgot the backticks

This currently doesn't work.

I have a string like this, where the row's data attribute is relied upon for some functionality.

`<tr class="row ${rowHiddenClass}" data-someId="${this.someId}">
  <td class="cell">${this.Notes}</td>
  <td class="cell amount">$${this.Amount}</td>
</tr>`

And it outputs like this within the attribute strings, which breaks that functionality.

<tr class="row $" data-someId="$">
  <td class="cell">A nice note</td>
  <td class="cell amount">$4.00</td>
</tr>

We may have to do some concatenation for the time being.
I'm not sure yet what the cleaner, simpler solution is.

This works.

`<tr class="row ` + rowHiddenClass + `" data-someId="` + this.someId + `">
  <!-- ... -->
</tr>`

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742362867a4429730.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信