...

View Full Version : enter into textfield when checkbox is checked



dakey
05-05-2004, 06:25 PM
I have a load of checkboxes with the value of different email addresses on them. Is there any script that when a user checks one or more of these checkboxes it would enter the email address into a textfield which is located on the same page.

many thanks

dakey

requestcode
05-05-2004, 06:49 PM
Something like this?


<html>
<head>
<title>Email Add</title>
<script type="text/javascript">
function doIt(obj)
{
document.myform.txt1.value+=" "+obj.value
}
</script>
</head>
<body>
<form name="myform">
<input type="checkbox" value="me@mail.com" onClick="doIt(this)">me@mail.com<br>
<input type="checkbox" value="you@mail.com" onClick="doIt(this)">you@mail.com<br>
<input type="checkbox" value="us@mail.com" onClick="doIt(this)">us@mail.com<br>
<input type="text" name="txt1" size="30">
</form>
</body>
</html>

glenngv
05-06-2004, 08:04 AM
That will add the same email address continually for every click even if you unchecked the checkboxes.

You may want to build around on this:


<html>
<head>
<title>Email Add</title>
<script type="text/javascript">
function doIt(obj)
{
if (obj.checked){
obj.form.txt1.value += ((obj.form.txt1.value!='') ? ',':'') + obj.value;
}
else {
obj.form.txt1.value = obj.form.txt1.value.replace(new RegExp(",*" + obj.value, "g"), '').replace(/^,*/, '');
}
}
</script>
</head>
<body>
<form name="myform">
<input type="checkbox" value="me@mail.com" onClick="doIt(this)">me@mail.com<br>
<input type="checkbox" value="you@mail.com" onClick="doIt(this)">you@mail.com<br>
<input type="checkbox" value="us@mail.com" onClick="doIt(this)">us@mail.com<br>
<input type="text" name="txt1" size="60">
</form>
</body>
</html>

dakey
05-06-2004, 12:37 PM
Thankyou that is perfect, works like a dream..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum