I’m doing one of the classic “replace the ampersands with ‘and'” problems. By using a function, the ampersand(s) in the text box need to change to “and” upon the textbox losing focus (onblur), and it must be able to do that with multiple ampersands. The twist that I’m dealing with is that it must work for two (or more) text boxes. I have it working fine with the code below, but is there any way to make it work without having to have two individual “for” statements? Thanks in advance!
[CODE]<?xml version = “1.0” encoding = “utf-8”?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns = “http://www.w3.org/1999/xhtml”>
<head>
<title>Replacing the Ampersands</title>
<script type=”text/javascript”>
function convertAnd(phrase)
{
var phrase1 = andform.phrase1.value;
for (i = 0; i < phrase1.length; i++)
{
andform.phrase1.value = andform.phrase1.value.replace(“&”,”and”);
}
var phrase2 = andform.phrase2.value;
for (i = 0; i < phrase2.length; i++)
{
andform.phrase2.value = andform.phrase2.value.replace(“&”,”and”);
}
}
</script>
</head>
<body onload=”andform.phrase1.focus();”>
<center>
<form name = “andform” action = “”>
<b><u>Look Ma, no &s!</b></u><br><br>
<p><label>Type a phrase in either text area that includes ampersands (&), then click on a random blank area on the page:<br>
<input name=”phrase1″ type=”text” size=”20″ onblur=”convertAnd(phrase1.value);”><br>
<input name=”phrase2″ type=”text” size=”20″ onblur=”convertAnd(phrase2.value);”></label></p>
<input type=”reset” value=”Clear” onclick=”andform.phrase.focus();”>
</form>
</center>
</body>
</html>