How to Get Selected Checkbox Value in Array Using jQuery

Websolutionstuff | May-24-2021 | Categories : PHP jQuery

In this post we will see how to get selected checkbox value in array using jquery. Here i will give you some example to store selected checkbox value in array using javascript.

When you want to get multiple selected checkbox values in array using jquery at that time you can use each function in javascript. Solet's see how to store selected checkbox value in array in javascript.

Example 1 : 

<html>
<head>
	<title>How to Get Selected Checkbox Value in Array Using jQuery - websolutionstuff.com
	</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

<body id="body">
	<h1>How to Get Selected Checkbox Value in Array Using jQuery - websolutionstuff.com </h1>
	<p id="msg"></p>
	
	<input type="checkbox" name="type" value="Green" /> Green:
	<input type="checkbox" name="type" value="Yello" /> Yello:
	<input type="checkbox" name="type" value="Blue" /> Blue:
	<input type="checkbox" name="type" value="Pink" />Pink:
	<br>
	<button>Submit</button>
	<p id="getvalue">
	</p>
	<script>
		$('#msg').text('please check checkbox then submit button.');
		$('button').on('click', function() {
			var array = [];
			$("input:checked").each(function() {
				array.push($(this).val());
			});
			$('#getvalue').text(array);
		});
	</script>
</body>

</html>

 

Example 2 : 

<html>
<head>
	<title>How to Get Selected Checkbox Value in Array Using jQuery - websolutionstuff.com</title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

</head>
<body>
  <table id="color">
    <tr>
        <td><input id="post1" type="checkbox" value="1"/><label for="Green"></label>Green</td>
    </tr>

    <tr>
       <td><input id="post2" type="checkbox" value="2"/><label for="Yellow">Yellow</label></td>
    </tr>

    <tr>
      <td><input id="post3" type="checkbox" value="3"/><label for="Blue">Blue</label></td>
    </tr>

    <tr>
      <td><input id="post4" type="checkbox" value="4"/><label for="Pink">Pink</label></td>
   </tr>
  </table><br />

  <input type="button" id="btnClick" value="Get" />

</body>

<script type="text/javascript">
    $(function () {
        $("#btnClick").click(function () {
            var selected = new Array();
            $("#color input[type=checkbox]:checked").each(function () {
               selected.push(this.value);
            });
             if (selected.length > 0) {
                alert("Selected values: " + selected.join(","));
            }
        });
    });
</script>
</html>

 

Recommended Post
Featured Post
How to Use Bitmasks for Efficient Data Filtering?
How to Use Bitmasks for Effici...

Data filtering might not sound like the most thrilling topic, but when it comes to processing large volumes of informati...

Read More

Oct-25-2023

How to Manage Time Zones in React.js Applications
How to Manage Time Zones in Re...

In the interconnected world of web development, where applications are accessed by users spanning multiple time zones, t...

Read More

Sep-04-2023

Fixed: Class "DOMDocument" Not Found In Laravel
Fixed: Class "DOMDocument" Not...

In this article, we will see to fixed class "DOMDocument" not found in laravel. Also, class 'domdocum...

Read More

Feb-17-2023

Laravel 8 Import Export CSV/EXCEL File Example
Laravel 8 Import Export CSV/EX...

In this article, we will see the laravel 8 import and export CSV/EXCEL file example. We will simple create imp...

Read More

Oct-01-2020