...

View Full Version : automatically select radiobutton when onclick in textfield



raf
12-31-2003, 12:25 AM
Is it possible to automaticaly select a radiobutton if a value is entered in a textfield?

The situation is: i'm loading a dynamically generated page (so i don't know the number of preceeding formfields) and inthere, i've got a group of 2 radiobuttons (both have the same name).
Radiobuton 1 is selected when the page loads.
Radiobutton 2 is followed by a textfield, and this radiobutton should be selected ( + radiobutton 1 is then no longer selected) if the users clicks inside the textfield.

any help appreciated.

fredmv
12-31-2003, 09:38 AM
Did you also want to make it so if the user clicks outside of the text field the other radio button becomes checked again (returned to default settings)? If so, here's how you'd do that:
<!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" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
</head>
<body>
<form action="#">
<input type="radio" name="rad" id="rad" checked="checked" />
<input type="radio" name="rad" id="rad" />
<input type="text" onfocus="elements[1].checked = true;" onblur="elements[0].checked = true;" />
</form>
</body>
</html>If not, here's how you can do what you originally wanted:
<!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" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
</head>
<body>
<form action="#">
<input type="radio" name="rad" id="rad" checked="checked" />
<input type="radio" name="rad" id="rad" />
<input type="text" onfocus="elements[1].checked = true;" />
</form>
</body>
</html>

raf
12-31-2003, 06:46 PM
Thanks for the reply, but this doesn't solve my problem --> which is that i can not use the sequense (like your elements[1]) of the formfield, because i don't know what that will be.

The form is dynamically build with PHP and depending on the userprofile, state of the form (first load, validated etc) and required action, there are other formfields included + the textfield isn't always the next one in the sequece.

I think my quick fix will be to solve it in my server side script (include an incrementing variable inside my PHP script that counts the number of included formfields before the radiobutton, and then insert that number as the reference in my textfields event handler.)

Thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum