Operations on checkbox using javascript

Get the value of checkbox which is inside form using javascript and get which checkbox have checked


Category: Java Script Tags: HTML, Java Script


Java Script tutorial 10

        Today I am going to show you how to perform some basic operations on checkbox using Java
Script.

<html>
<head>
<script type="text/javascript">
function change()
{
var s = "You like ";
var cntr = document.form1;
if(cntr.check1.checked)
s += cntr.check1.value;
if(cntr.check2.checked)
s += " " + cntr.check2.value;
alert(s);
}
</script>
</head>
<body>
<form action="" name="form1">
<input type="checkbox" name="check1" value="CD">CD</input>
<input type="checkbox" name="check2" value="DVD">DVD</input>

</form>
<input type="button" onclick="change()" value="Like" />
</body>
</html>

        In above example we created two checkbox check1 and check2 inside form form1 the value of check1 is CD and check2 is DVD and a button bounded with javascript function change() on the onclick event. When user checks any one or both boxes and clicks the button then change() function gets called and alert the value of checked boxes.
        We can get which checkbox is checked or not by document.form1.check1.checked and for value use document.form1.check1.value.


Like 1 Person
Last modified on 29 August 2018
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 132
Questions: 9
Given Best Solutions: 9 *


You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x