/    Sign up×
Community /Pin to ProfileBookmark

Slight twist on replacing ampersands

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>[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meMar 03.2009 — [CODE]onblur="convertAnd(this);"

// and your new function (no need for loops):


function convertAnd(phrase){
phrase.value = phrase.value.replace(/&/g,"and");
}

[/CODE]
Copy linkTweet thisAlerts:
@perkykoalaauthorMar 04.2009 — Thanks for the tip! Just out of curiosity, what does the /&/g part mean? How come putting quotes around the ampersand doesn't cut it?
Copy linkTweet thisAlerts:
@rpgfan3233Mar 04.2009 — The replace() function takes a RegExp object as its first argument, not a String. /&/g is like saying [i]new RegExp("&", "g")[/i].
×

Success!

Help @perkykoala 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...