Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript and apostrophes

Status
Not open for further replies.

iandobrien

Programmer
May 2, 2002
7
IE
if you have a sentence that you are trying to print to your screen using javascript, how can you make sure the sentence prints out even if it contains an aposthrophe??For instance someone might enter a sentence and we're not sure whether that sentence will contain any apostrophes but what we want is to make sure the sentence prints out as it was written in (even if it contains apostrophes!)thanks,
 
umm i think this is what you want..im sure theres a better way to do this (i just cant think of it ATM).
Code:
<html>
<head>
<Script language='javascript'><!--
var x,y,apost; // initiate variables
function checkvar(name)
{
if (name.indexOf(&quot;'&quot;) != -1) // if an apostrophe 
//detected in the sentence
{
x = name.split(&quot;'&quot;); // split the sentence up by 
//the apostrophes
y = x[0]+&quot;\'&quot;; // add the first part of the sentence to y
for (a = 1;a < x.length-1; a++){
y += x[a]+&quot;\'&quot;; // add the middle of the sentence to 
// y, adding \'
}
y += x[x.length-1]; // add last part of sentence to y
document.write(y); // write the sentence to the page
}
else apost = 'no';
}
//--></script>
</head>
<body><form name='form1'>
<input type='text' name='input1' onBlur='checkvar(this.value)'> <!--after clicking out
of this text box it passes its value to the javascript function-->

</form>
</body>
</html>
hope this helps Suceess, thats the way you spell success!
Got a question? Ask IMOZRMY!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top