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:

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... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)