Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts

    automatically select radiobutton when onclick in textfield

    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.

  • #2
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <!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:
    Code:
    <!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>

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •