PDA

View Full Version : Problem with ' and " quotes


Evario
11-20-2007, 08:30 AM
Hi have the following code:


var quote = document.getElementById('acc_quoteInfo').innerHTML;

document.getElementById(id).deleteCell(0);
document.getElementById(id).deleteCell(0);

oCell = document.getElementById(id).insertCell(-1);
oCell.innerHTML = '<div style="margin-left: 73px; margin-top: 20px; color: #999999;">- Maximum 50 characters allowed.<br /> - Submit with a blank field to clear your quote.</div><form name="Account_Quote" id="Account_Quote"><div id="acc_emailCurrent">New Quote:<input name="quote" type="text" maxlength="50" class="acc_emailField" style="margin-left: 37px;" /></div><div class="acc_email" style="margin-top:10px;"><a style="width: 30px; font-size:20px; color:#99B731;" href="#" onclick="validateAccount(\'quote\'); return false;">Update</a></div><div class="acc_email" id="acc_quoteValidate" style="margin-top: 10px; color:#FF0000;"></div>';

document.getElementById('acc_quoteC').innerHTML = '<span style="cursor:pointer;" onclick="details_hide(\'acc_quote\', \''+quote+'\')">Hide</span>';


I have highlighted the problem code in red. Basically the var quote will be something of the form "This is my quote" (with the quotation marks) and this produces an error the way I have it coded because the javascript gets confused with the ' and " characters. How do I fix this?

Thanks,
Marc

Philip M
11-20-2007, 08:40 AM
var mytext = "\"This is a quote\""
alert (mytext);

shyam
11-20-2007, 08:48 AM
since the bounding quotes are ' in this case escape all instances of ' with \'
document.getElementById('acc_quoteC').innerHTML = '<span style="cursor:pointer;" onclick="details_hide(\'acc_quote\', \''+quote.replace(/'/g, '\\\'')+'\')">Hide</span>';