简体   繁体   中英

call a function with single quotes

I have to call the following javascript function

function changeExampleField(firstValue, changeId, field, wordToChange)
{
 var exampleTxtElem = document.getElementById(changeId);
 var newTxt = field.value;
 exampleTxtElem.innerHTML = firstValue.replace(wordToChange, newTxt);
}

The problem at this is, I get the value of "firstValue" from the database, it is a String and it can contain single quotes. For example:

I get a value from the database which operates

<input class="" type="text" name="text_label.test_virus_connection" value="test" size="50" maxlength="50" id="a_text_label.test_virus_connection"   onkeyup="changeExampleField('Verbindung testen', 'example_label.test_virus_connection', this, 'test')"  onFocus="dispHelp('')">

But i can get values from the database which dont operates

<input class="" type="text" name="text_pass_sprachtest.label.execute.successfull" value="test" size="50" maxlength="50" id="a_text_pass_sprachtest.label.execute.successfull"   onkeyup="changeExampleField('Bewerbung(en) erfolgreich auf 'Sprachtest(s) bestanden' gesetzt!', 'example_pass_sprachtest.label.execute.successfull', this, 'test')"  onFocus="dispHelp('')">

My question is: is there a different way to call the function (for example with properties or so)? Or a different way to solve this problem?

You should do either of this

replace outer single quotes with double quote

"Bewerbung(en) erfolgreich auf 'Sprachtest(s) bestanden' gesetzt!"

or escape the single quotes in middle

'Bewerbung(en) erfolgreich auf \\'Sprachtest(s) bestanden\\' gesetzt!'

if you do not do ths either way then its not considered as string

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