/    Sign up×
Community /Pin to ProfileBookmark

JS to fix special chars in <textarea> does not work

Hi I have downloaded the code to allow special html characters in <textarea> be replaced with the actual code so it does not cause problem when the text is submitted into the database. For example ‘>’ to be replaced with ‘&gt;’ So for a string ‘ 3 > 2’ I expect to be replaced with ‘3 &gt; 2; in the text box. However it does not work.
I put in the alert statements and it shows that the js function is being invoked, but after the function is done, the special characters in the text box remain the same. Can someone help?

<html>
<header>
</header>
<body>
<script language=”javascript”>

function html2entities(){
var re=/[(<>”‘&]/g
for (i=0; i<arguments.length; i++)
alert(“check char”+ arguments[i].value);
arguments[i].value=arguments[i].value.replace(re, function(m){return replacechar(m)})
}

function replacechar(match){
if (match==”<“)
return “&lt;”
else if (match==”>”)
return “&gt;”
else if (match==”””)
return “&quot;”
else if (match==”‘”)
return “‘”
else if (match==”&”)
return “&amp;”
}

</script>

<form>
Data 1
<textarea name=”data1″ style=”width: 400px; height: 100px”></textarea><br />
<input type=”button” onClick=”html2entities(this.form.data1)” value=”Convert special chars to entities”>
</form>
</body>
</html>

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @sportyNerd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...