PHP

how to get checked checkbox value using jquery

You can use the jQuery :checked selector together with the each() method to retrieve the values of all checkboxes selected during a group. The each() method used here simply iterates over all the checkboxes that are checked. Let’s try an example to check how it works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>how to get checked checkbox value using jquery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $("button").click(function(){
            var favorite = [];
            $.each($("input[name='sport']:checked"), function(){
                favorite.push($(this).val());
            });
            alert("My favourite sports are: " + favorite.join(", "));
        });
    });
</script>
</head>
<body>
    <form>
        <h3>Select your favorite sports:</h3>
        <label><input type="checkbox" value="football" name="sport"> Football</label>
        <label><input type="checkbox" value="baseball" name="sport"> Baseball</label>
        <label><input type="checkbox" value="cricket" name="sport"> Cricket</label>
        <label><input type="checkbox" value="boxing" name="sport"> Boxing</label>
        <label><input type="checkbox" value="racing" name="sport"> Racing</label>
        <label><input type="checkbox" value="swimming" name="sport"> Swimming</label>
        <br>
        <button type="button">Submit</button>
    </form>
</body>
</html>
Basant

Recent Posts

How to Add Auto Increment to a Column in PostgreSQL ?

Learn how to efficiently set up auto-increment columns in PostgreSQL with this step-by-step guide. From…

4 hours ago

How to get the value of selected radio button using jQuery

Just use below code and modified according to your need. Note: this is very useful…

3 months ago

AWS

Steps: AWS puttyubuntu sudo su apt update apt install apache2 service apache2 start service apache2…

8 months ago