get multiple selected checkbox value in javascript code example
Example: get multiple checkbox value in javascript
<!DOCTYPE html>
<html>
<body>
<label>Check Which you have</label><br/>
<input type="checkbox" id="checkbox1" name="bike" value="Bike"> Bike <br/>
<input type="checkbox" id="checkbox2" name="car" value="Car"> Car <br/>
<input type="checkbox" id="checkbox3" name="home" value="Home"> Home <br/>
<button onclick="getValue()">Get Value</button>
<p id="p1"></p>
<script>
function getValue() {
var ele=[]
var bike = document.getElementById("checkbox1")
if(bike.checked){
ele.push(bike.value);
}
var car = document.getElementById("checkbox2")
if(car.checked){
ele.push(car.value);
}
var home = document.getElementById("checkbox3")
if(home.checked){
ele.push(home.value);
}
if(ele.length>0){
document.getElementById("p1").innerHTML = ele;
}
else{
document.getElementById("p1").innerHTML = "You Dont have any thing";
}
}
</script>
</body>
</html>