简体   繁体   中英

Escaping double quotes in JavaScript

I have this code:

 facetsString += "<td><input type='checkbox' value=facetList[count].term>&nbsp;&nbsp;" + facetList[count].term +  " (" + facetList[count].count + ")" + "</td>";

I'm trying to give each checkbox a unique value facetList[count].term , but I don't know how to escape the double quotes...

Just put a backslash in front of the double-quotes:

facetsString += "<td><input type='checkbox' value=\"facetList[count] ... \" /></td>"

Alternatively, you can wrap the outer in single quotes, and use double quotes for property values:

facetsString += '<td><input type="checkbox" value="facetList[count] ... " /></td>'

You can escape double quotes like this:

"string with \"double qoutes\""

The solution is:

facetsString += "<td><input type='checkbox' value=\"" + facetList[count].term + "\">&nbsp;&nbsp;" + facetList[count].term + " (" + facetList[count].count + ")" + "</td>";

The example provided would write facetList[count].term in the value attribute, and not the actual value of the variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM