How to get all selected checkboxes in an array using jQuery ?

Given a number of checkboxes. The task is to get the value of all selected checkboxes in the array using JQuery. Few of the methods are discussed below:

  • jQuery on() Method
    This method adds one or more event handlers for the selected elements and child elements.
    Syntax:

    $(selector).on(event, childSel, data, fun, map)
    

    Parameters:

    • event: This parameter is required. It specifies one or more than one event(s) or namespaces to add to the selected elements.
      If there are multiple event values, separate them by space. Event must be a valid.
    • childSel: This parameter is optional. It specifies that the event handler should only be attached to the defined child elements.
    • data: This parameter is optional. It specifies additional data to pass to the function.
    • fun: This parameter is required. It specifies the function to run when the event occurs.
    • map: It specifies an event map ({event:func(), event:func(), …}) having one or more event to add to the selected elements, and functions to run when the events happens.
  • jQuery text() Method
    This method set/return the text content of the selected elements.
    If this method is used to return content, it provides the text content of all matched elements (HTML tags will be removed).
    If this method is used to set content, it replace the content of ALL matched elements.
    Syntax:

    • Return text content:

      $(selector).text()
      
    • Set text content:

      $(selector).text(content)
      
    • Set text content using a function:

      $(selector).text(function(index, curContent))
      

    Parameters:

    • content: This parameter is required. It specifies the new text content for the selected elements.
    • function(index, curContent): This parameter is optional. It specifies a function that returns the new text content for the selected elements.
      • index: It returns the index position of the element in the set.
      • curContent: It returns current content of selected elements.
  • jQuery val() Method
    This method return/set the value attribute of the matched elements.
    In case of return value, This method returns the value attribute’s value of the FIRST selected element.
    In case of set value, This method sets the value attribute’s value for ALL selected elements.
    Syntax:

    • Return the value attribute:

      $(selector).val()
      
    • Set the value attribute:

      $(selector).val(value)
      
    • Set the value attribute using a function:

      $(selector).val(function(index, cValue))
      

    Parameters:

    • value: This parameter is required. It specifies the value attribute’s value.
    • function(index, cValue): This parameter is optional. It specifies a function that returns the value to set.
      • index: It returns the index of the element in the set.
      • cValue: It returns the current value attribute of selected elements.
  • JavaScript Array push() Method
    This method adds new items at the end of array, and returns the new length.
    Syntax:

    array.push(item1, item2, ..., itemN)
    

    Parameters:

    • item1, item2, …, itemN: This parameter is required. It specifies the item(s) to add to the array.

    Return value:
    It returns a number, which represents the new length of the array.

Example 1: This example selects the the checked checkboxes by a selector and then calls the .each() method to each element and finally push them into the array.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JQuery | Get all selected checkboxes in an array.
    </title>
    <style>
        #GFG_UP {
            font-size: 17px;
            font-weight: bold;
        }
          
        #GFG_DOWN {
            color: green;
            font-size: 24px;
            font-weight: bold;
        }
          
        button {
            margin-top: 20px;
        }
    </style>
</head>
<script src=
</script>
  
<body style="text-align:center;" id="body">
    <h1 style="color:green;">  
            GeeksforGeeks  
        </h1>
    <p id="GFG_UP">
    </p>
      
    <input type="checkbox" name="type" value="GFG" /> GFG:
    <input type="checkbox" name="type" value="Geeks" /> Geeks:
    <input type="checkbox" name="type" value="Geek" /> Geek:
    <input type="checkbox" name="type" value="portal" /> portal:
    <br>
    <button>
        click here
    </button>
    <p id="GFG_DOWN">
    </p>
    <script>
        $('#GFG_UP')
        .text('First check few elements then click on the button.');
        $('button').on('click', function() {
            var array = [];
            $("input:checkbox[name=type]:checked").each(function() {
                array.push($(this).val());
            });
            $('#GFG_DOWN').text(array);
        });
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
  • After clicking the button:

Example 2: This example also does the same work but with a different approach. This example selects the the checked checkboxes by a selector(different from the previous one) and then calls the .each() method to each element and finally push them into the array.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JQuery | Get all selected checkboxes in an array.
    </title>
    <style>
        #GFG_UP {
            font-size: 17px;
            font-weight: bold;
        }
          
        #GFG_DOWN {
            color: green;
            font-size: 24px;
            font-weight: bold;
        }
          
        button {
            margin-top: 20px;
        }
    </style>
</head>
<script src=
</script>
  
<body style="text-align:center;" id="body">
    <h1 style="color:green;">  
            GeeksforGeeks  
        </h1>
    <p id="GFG_UP">
    </p>
      
    <input type="checkbox" name="type" value="GFG" /> GFG:
    <input type="checkbox" name="type" value="Geeks" /> Geeks:
    <input type="checkbox" name="type" value="Geek" /> Geek:
    <input type="checkbox" name="type" value="portal" />portal:
    <br>
    <button>
        click here
    </button>
    <p id="GFG_DOWN">
    </p>
    <script>
        $('#GFG_UP')
        .text('First check few elements then click on the button.');
        $('button').on('click', function() {
            var array = [];
            $("input:checked").each(function() {
                array.push($(this).val());
            });
            $('#GFG_DOWN').text(array);
        });
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
  • After clicking the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.