Javascript example to get comma separated checked value from multi checkbox
Below are the example about how to get comma saperated value of selected checkbox through javascript function.
<html> <head> <title>Getting checked values of Multiple Checkbox by javascript</title> </head> <body> <form name="frmcheckbox" > <input type="checkbox" id="1" name="service_offered[]" value="1" onclick="getOfffered_Service(this)"> Authorized Sales<br> <input type="checkbox" id="2" name="service_offered[]" value="2" onclick="getOfffered_Service(this)"> Service<br> <input type="checkbox" id="3" name="service_offered[]" value="3" onclick="getOfffered_Service(this)"> Rentals<br> <input type="checkbox" id="4" name="service_offered[]" value="4" onclick="getOfffered_Service(this)"> Online Store <br>Your selected checkbox value is <input type="text" id="t" name="t"> </form> <script language="javascript" type="text/javascript"> function getOfffered_Service(chkbox) { var chks = document.getElementsByName('service_offered[]'); var checkCount = 0; var text = new Array(); var strtext = ""; for (var i = 0; i < chks.length; i++) { if (chks[i].checked) { var arlength = text.length; text[arlength] = chks[i].value; } } strtext = text.join(","); document.getElementById("t").value = strtext ; } </script> </body> </html>
Related posts:
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments