html - Need third kind of quotation marks in JavaScript - Stack Overflow

I'm changing the HTML using javascript. Therefore, I have a String that saves the HTML code, since

I'm changing the HTML using javascript. Therefore, I have a String that saves the HTML code, since it is produced in a for-loop. In it there is an <a> tag. To the <a> tag I want to add an onClick which calls a function with a parameter which is saved in a javascript variable.

let parameter = "p1";
let to_html_String = "<a href='#' onClick='create_sprite_window("+parameter+")'></a>";
document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;

This does not work because it would prodece the following html:

<a href='#' onClick='create_sprite_window(p1)'></a>

It does not work because there are no quotation marks before and after p1. My problem is that I would need a third kind of quotation marks in order to solve this. In the following example I will use # where I would need those quotation marks:

let parameter = "p1";
let to_html_String = "<a href='#' onClick='create_sprite_window(#"+parameter+"#)'></a>";
document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;

I can't use a single quotation mark because it would end the onClick and I can't use double beacuse it would end to_html_String.

Are there third quotation marks or is there an other way to solve this?

I'm changing the HTML using javascript. Therefore, I have a String that saves the HTML code, since it is produced in a for-loop. In it there is an <a> tag. To the <a> tag I want to add an onClick which calls a function with a parameter which is saved in a javascript variable.

let parameter = "p1";
let to_html_String = "<a href='#' onClick='create_sprite_window("+parameter+")'></a>";
document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;

This does not work because it would prodece the following html:

<a href='#' onClick='create_sprite_window(p1)'></a>

It does not work because there are no quotation marks before and after p1. My problem is that I would need a third kind of quotation marks in order to solve this. In the following example I will use # where I would need those quotation marks:

let parameter = "p1";
let to_html_String = "<a href='#' onClick='create_sprite_window(#"+parameter+"#)'></a>";
document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;

I can't use a single quotation mark because it would end the onClick and I can't use double beacuse it would end to_html_String.

Are there third quotation marks or is there an other way to solve this?

Share Improve this question asked Aug 3, 2016 at 9:19 niklasscniklassc 5502 gold badges13 silver badges29 bronze badges 1
  • 1 you can also use escape char ... \' or \" as appropriate – Jaromanda X Commented Aug 3, 2016 at 9:23
Add a ment  | 

4 Answers 4

Reset to default 5

If you're using ES6, you can use template strings like

let foo = `Hello "World's"`;

Anyway - why wouldn't you just escape your quotation marks ?

let bar = 'hello \' world';
let buz = "hello \" world";

Don’t build HTML using JavaScript. The DOM API will let you build document nodes cleanly and safely, and even attach event listeners so you don’t have to build JavaScript strings in the HTML you’re building in JavaScript.

let parameter = "p1";

const link = document.createElement('a');
link.href = '#';
link.textContent = 'maybe you want something in this link?';
link.addEventListener('click', function (e) {
    e.preventDefault();
    create_sprite_window(parameter);
});

document.getElementById('sidebar_left_sprites')
    .appendChild(link);
  • createElement
  • addEventListener
  • appendChild

This will work

"<a href='#' onClick='create_sprite_window('"+parameter+"')></a>";

/* Use \' to differentiate with function single quote */

let parameter = "p1"; let to_html_String = ""; document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;

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

相关推荐

  • html - Need third kind of quotation marks in JavaScript - Stack Overflow

    I'm changing the HTML using javascript. Therefore, I have a String that saves the HTML code, since

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信