|
Prev: Read a file into Javascript variable from the server
Next: reusable function to get radio button's selected value?
From: Jeff on 21 Apr 2008 06:58 I'm writing innerHTML for a text field. Sometime the textfield has double quotes in them. value_=value_.replace(/"/g,'\\"'); var content = '<input type="text" name="'+name_+'" size="'+size_+'" value="'+value_+'">'; I'm not sure why that doesn't work, but it doesn't! I'm missing something simple, somewhere. Jeff
From: Erwin Moller on 21 Apr 2008 09:12 Jeff schreef: > I'm writing innerHTML for a text field. > > Sometime the textfield has double quotes in them. > > value_=value_.replace(/"/g,'\\"'); Hi, Unless you really know what you are doing, I advise you to simply use the codes for special HTML chars. eg: <script type="text/javascript"> var org = "test single' and double\" endtest"; // replace " replaced = org.replace(/\"/g,'"'); // replace ' replaced = replaced.replace(/\'/g,'''); var content = '<br><input type="text" name="bla" size="30" value="'+replaced+'">'; document.write(content); </script> You can do this with adding backslashes, but things gets complicated very fast, especially when passing the strings around. Things get worse when you also do this serverside. No need to dive into that when you have " and ' to do the work for you. Regards, Erwin Moller > var content = '<input type="text" name="'+name_+'" size="'+size_+'" > value="'+value_+'">'; > > I'm not sure why that doesn't work, but it doesn't! I'm missing > something simple, somewhere. > > Jeff
From: Bart Van der Donck on 21 Apr 2008 10:19 Erwin Moller wrote: > <script type="text/javascript"> > var org = "test single' and double\" endtest"; > // replace " > replaced = org.replace(/\"/g,'"'); > // replace ' > replaced = replaced.replace(/\'/g,'''); > > var content = '<br><input type="text" name="bla" size="30" > value="'+replaced+'">'; > > document.write(content); > > </script> > > You can do this with adding backslashes, but things gets complicated > very fast, especially when passing the strings around. Things get worse > when you also do this serverside. Can't be done with backslashes; even \x22 or \u0022 are not accepted. " is the only way. -- Bart
From: Jeff on 21 Apr 2008 12:52 Bart Van der Donck wrote: > Erwin Moller wrote: > >> <script type="text/javascript"> >> var org = "test single' and double\" endtest"; >> // replace " >> replaced = org.replace(/\"/g,'"'); >> // replace ' >> replaced = replaced.replace(/\'/g,'''); >> >> var content = '<br><input type="text" name="bla" size="30" >> value="'+replaced+'">'; >> >> document.write(content); >> >> </script> >> >> You can do this with adding backslashes, but things gets complicated >> very fast, especially when passing the strings around. Things get worse >> when you also do this serverside. > > Can't be done with backslashes; even \x22 or \u0022 are not accepted. > " is the only way. Thanks! Jeff > > -- > Bart
From: Thomas 'PointedEars' Lahn on 21 Apr 2008 16:12
Bart Van der Donck wrote: > Erwin Moller wrote: >> [escape the value of `replaced' for Valid markup] >> var content = '<br><input type="text" name="bla" size="30" >> value="'+replaced+'">'; >> >> document.write(content); >> [...] >> >> You can do this with adding backslashes, but things gets complicated >> very fast, especially when passing the strings around. Things get worse >> when you also do this serverside. > > Can't be done with backslashes; even \x22 or \u0022 are not accepted. > " is the only way. `"', `"', or using other DOM mutator methods are other ways. PointedEars -- realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann |