JavaScript: Event Handling for radio button

Source: Internet
Author: User

Radio Button Events:
Radio button is a multi-select process, but radio button is also an HTML element, so it represents an object for JavaScript.
Example: Obtaining a radio button data
<!doctype html>


<meta charset= "Utf-8" >
<meta name= "description" content= "All kinds of input" >
<meta name= "keywords" content= "input,html" >

<title> radio button Testing </title>

<script type= "Text/javascript" >
window.onload = function () {
document.getElementById (' showbtn '). AddEventListener (' click ', Function () {
var Sexarr = document.all (' sex ');
if (sexarr[0].checked) {
alert ("Gender is:" + sexarr[0].value);
}else{
alert ("Gender is:" + sexarr[1].value);
}

},false);
}
</script>


<body>
<form action= "" >
Gender: <input type= "Radio" name= "Sex" id= "sex" value= "male-male" checked= "yes" > male
<input type= "Radio" name= "Sex" id= "Sex" value= "female-women" > Women <br/>
<input type= "button" id= "showbtn" value= "select" ></input>
</form>
</body>

Code:

<!doctype html>window.onload=function() {document.getElementById (' Showbtn '). AddEventListener (' click ',function() {                varSexarr = document.all (' Sex '); if(sexarr[0].checked) {alert ("Sex is:" + sexarr[0].value); }Else{alert ("Sex is:" + sexarr[1].value); }                           },false); }    </script> Gender:<input type= "Radio" name= "Sex" id= "sex" value= "male-Men" checked= "yes" >male<input type= "Radio" name= "Sex" id= "Sex" value= "female-women" > Women <br/> <input type= "button" id= "s Howbtn "value=" select "></input> </form> </body>

The default gender is male, as follows:

When selecting male, the box is as follows:

When choosing a female, the box is as follows

JavaScript: Event Handling for radio button

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.