I want to use the var s
for the value-field of an option-element. How to escape correctly in the sense to contain user input used for key and value?
var key='highway'; var value='track';
var s = JSON.stringfy({ [key]:value }, undefined,'');
s = s.replace(/'/g,'\\\'').replace(/"/g,'\\"');
var h = '<option type="text" value="' + s + '">'+key+'</option>';
// ...
document.getElementById('aSelectElement').innerHTML = h;
Edit: Actually I use it for an option-Element instead of an input element as mentioned first.
If I select the result in the browser, and let it me show the generated html, it shows something like:
<option value="{\" highway\":\"track\"}"="">highway track</option>
Which adds the question, why there is a whitespace in front of the highway?