Jump to content

How can I put focus on a radio button ?


dooballoh

Recommended Posts

How can I put focus on a radio button on "return false" ?Example validation codes;

var radioSelected = false; for (var i=0; i<document.formname.gender.length; i++) 	{  	if (document.formname.gender[i].checked) radioSelected = true; 	} 	if (!radioSelected) 	{  	alert('message goes here'); 					I'd like to put focus here.focus(); 									return false;						}

Thank you in advanse.

Link to comment
Share on other sites

<form name="formname">Male: <input type="radio" name="gender" value="Male" >Female: <input type="radio" name="gender" value="Female" /><input type="Submit" value="submit" onclick="get_selected();"></form><script type="text/javascript">function get_selected(){ var radioSelected = false; for (var i=0; i<document.formname.gender.length; i++) { if (document.formname.gender.checked) radioSelected = true; } if (!radioSelected) { for (var i=0; i<document.formname.gender.length; i++) { if (!document.formname.gender[0].checked) { document.formname.gender[0].checked = true document.formname.gender[0].focus(); } } }}</script>I am not gettinf the perfect result as u suggest. U can check while u run above code.If u solve that problem then please reply me.

Link to comment
Share on other sites

Thank you very much Ankit Shah.Hahaha it's works great. I have an another question.How can I do either highlight, or put border(red) outside of the first radio button(if not possible whole radio group)?So that users can easily see what they are missing.Example HTML;Male: <input type="radio" name="gender" value="Male" />Female: <input type="radio" name="gender" value="Female" />Either 1. highlight, or red border on "Male" radio button2. red border around whole "Male" and "Female" radio group.Thank you very much in advance.

Link to comment
Share on other sites

Thank you very much Ankit Shah.Hahaha it's works great. I have an another question.How can I do either highlight, or put border(red) outside of the first radio button(if not possible whole radio group)?So that users can easily see what they are missing.Example HTML;Male: <input type="radio" name="gender" value="Male" />Female: <input type="radio" name="gender" value="Female" />Either 1. highlight, or red border on "Male" radio button2. red border around whole "Male" and "Female" radio group.Thank you very much in advance.
hi, try:<input type="radio" ....... style= bgcolor:"red">or<input type="radio" ....... style= color:"red">and let me know if it worked plz.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...