Connecting Tech Pros Worldwide Forums | Help | Site Map

" in textfields

Jeff
Guest
 
Posts: n/a
#1: Jun 27 '08
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

Erwin Moller
Guest
 
Posts: n/a
#2: Jun 27 '08

re: " in textfields


Jeff schreef:
Quote:
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,'&quot;');
// 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 &quot; and ' to do the work
for you.

Regards,
Erwin Moller

Quote:
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
Bart Van der Donck
Guest
 
Posts: n/a
#3: Jun 27 '08

re: " in textfields


Erwin Moller wrote:
Quote:
<script type="text/javascript">
var org = "test single' and double\" endtest";
// replace "
replaced = org.replace(/\"/g,'&quot;');
// 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.
&quot; is the only way.

--
Bart
Jeff
Guest
 
Posts: n/a
#4: Jun 27 '08

re: " in textfields


Bart Van der Donck wrote:
Quote:
Erwin Moller wrote:
>
Quote:
><script type="text/javascript">
>var org = "test single' and double\" endtest";
>// replace "
>replaced = org.replace(/\"/g,'&quot;');
>// 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.
&quot; is the only way.
Thanks!

Jeff
Quote:
>
--
Bart
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#5: Jun 27 '08

re: " in textfields


Bart Van der Donck wrote:
Quote:
Erwin Moller wrote:
Quote:
>[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.
&quot; is the only way.
`&#x22;', `"', 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
Erwin Moller
Guest
 
Posts: n/a
#6: Jun 27 '08

re: " in textfields


Thomas 'PointedEars' Lahn schreef:
Quote:
Bart Van der Donck wrote:
Quote:
>Erwin Moller wrote:
Quote:
>>[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.
>&quot; is the only way.
>
`&#x22;', `"', or using other DOM mutator methods are other ways.
>
>
PointedEars
Hi Thomas and Bart,

When Bart wrote that it couldn't be done, I had this feeling he was wrong.
I am positive I did it earlier, I thought.
So I retried, and was unsuccesfull.
So I looked up some old code where I did it, only to find out I didn't
do it that way. :-)
I weaseled out: writing the HTML, then some JavaScript that filled the
value. :-/

Anyway, writing HTML with formelements like that is ugly IMHO.
Since all my pages are produced by PHP I seldom need document.write,
most/all can be done at the server.

Regards,
Erwin Moller
Jeff
Guest
 
Posts: n/a
#7: Jun 27 '08

re: " in textfields


Erwin Moller wrote:
Quote:
Thomas 'PointedEars' Lahn schreef:
Quote:
>Bart Van der Donck wrote:
Quote:
>>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.
>>&quot; is the only way.
>>
>`&#x22;', `"', or using other DOM mutator methods are other ways.
>>
>>
>PointedEars
>
Hi Thomas and Bart,
>
When Bart wrote that it couldn't be done, I had this feeling he was wrong.
I am positive I did it earlier, I thought.
So I retried, and was unsuccesfull.
So I looked up some old code where I did it, only to find out I didn't
do it that way. :-)
I weaseled out: writing the HTML, then some JavaScript that filled the
value. :-/
>
Anyway, writing HTML with formelements like that is ugly IMHO.
Since all my pages are produced by PHP I seldom need document.write,
most/all can be done at the server.
The trend is the other way, not with document.write but with either
innerHTML or DOM methods.

Say we have a large table of data and you just want to edit an item
here and there. It's easier for the user if he can click on the item he
wants to edit and have that replaced with a textfield or some such and
the updates would be returned to the server in the background using
AJAX. At least that is what I was doing... simple stuff...

Jeff



Quote:
Regards,
Erwin Moller
Closed Thread