Forum Moderators: open

Message Too Old, No Replies

Apostrophe Problem - What's Next

         

aax123

10:18 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



When I step into showPreview(vid) and the value in the options includes a ' it fails. i.e. What's Next

Any idea how to fix this in JavaScript? I know that in vb i can use Replace(var, "'", "''") but am not sure how to do it in JavaScript.
-----------------------------

oNewTd13.innerHTML = "<select class=copy style='background-color: #FFFFFF' onchange=\"document.forms[0].elements['linerTextAndDescriptionAdj_" + vid + "'].value = linerText + ' ' + this.options[this.selectedIndex].innerHTML; document.forms[0].elements['DescriptionAdj_" + vid + "'].value = this.options[this.selectedIndex].innerHTML; \" name=descript_" + vid + "><option value=''>-- choose description --</option>" + descriptionAdjOptions + "</select>";

function showPreview(vid) {
var oForm = document.forms[0];
var iNumElems = oForm.elements.length;
var oElem;

// Refresh the inner contents of divPreview with the most recent copy of linerText.
if (eval("document.forms[0].elements['linerTextAndDescriptionAdj_" + vid + "'].value")!= "") {
eval("divPreview" + vid + ".innerText = '" + eval("document.forms[0].elements['linerTextAndDescriptionAdj_" + vid + "'].value") + "'");
}
eval("divPreview" + vid + ".style.visibility = 'visible'");
for (var i=0;i<iNumElems;i++) {
oElem = oForm.elements[i];
if (("SELECT" == oElem.tagName) && (oElem.name!= "descript_" + vid)){
oElem.style.visibility = 'hidden';
}
}
}

Bernard Marx

10:30 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't really make my way through all that. One way to get around confusing things could be to use DOM methods to populate elements, not innerHTML. Tht way you avoid all the crazy quoting.

Remove all single quotes in a string:
(need a RegExp else only the first will be removed)

str = str.replace(/'/g,'')
// could maybe escape the quote in the Regexp \', if your syntax highlighting goes off.

Don't use eval (more crazy quoting games)


eval("document.forms[0].elements['linerTextAndDescriptionAdj_" + vid + "'].value")
// to
document.forms[0].elements['linerTextAndDescriptionAdj_' + vid].value

AND (!)


eval("divPreview" + vid + ".innerText = '" + eval("document.forms[0].elements['linerTextAndDescriptionAdj_" + vid + "'].value") + "'");

// to

document.getElementById('divPreview' + vid ).innerText
= document.forms[0].elements['linerTextAndDescriptionAdj_' + vid ].value;


..but it's pretty hard to be sure about that one.

aax123

1:36 am on Sep 23, 2005 (gmt 0)

10+ Year Member



Thanks,