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>
The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...