Frank_Vr
Frank_Vr

Reputation: 659

issue with apostrophes and double quotes in Javascript

I'm having trouble adding in a variable into the following line of JavaScript:

 row.insertCell(0).innerHTML = "<div onClick='Myfunction(" +  Password +  ");'></div>"

how do I add in the password variable I'm getting confused with apostrophes and double quotes

I think it needs to put the value in-between apostrophes but this clashes with what's already there?

Upvotes: 0

Views: 175

Answers (3)

Ramselvaraj
Ramselvaraj

Reputation: 112

Try this example:

 var Password ='sample';

 document.getElementById("id1").value= '<div onClick="Myfunction(\'' +  Password +  '\');"></div>';

 alert(document.getElementById("id1").value);

This is called Escaping. Use backslash() for the character which you want to escape.

Upvotes: 1

Grundy
Grundy

Reputation: 13380

you can try escape quotes with backslashes like this

row.insertCell(0).innerHTML = "<div onClick='Myfunction(\"" +  Password +  "\");'></div>"

Upvotes: 1

Maysam
Maysam

Reputation: 7367

Try this: row.insertCell(0).innerHTML = "<div onClick=Myfunction('" + Password + "');></div>"

Upvotes: 1

Related Questions