skip to Main Content

i am trying to get array from checked buttons when i clicked a button and i also want to get the value of the clicked button i am able to get the array part but i am unable to get the value of the button

below is my code

$('.multiple').on("click", function () {
        var action = $(this).val();
        var yourArray = $("input:checkbox[name=type]:checked").map(function () { return $(this).val() }).get()


        console.log(action);
        var jsonString = JSON.stringify(yourArray);
        console.log(jsonString);
        $.ajax({
            type: "POST",
            url: "../sales/script.php",
            data: { data: jsonString, action: action },
            cache: false,

            success: function (response) {
                console.log(response);
            }
        });

    });

what i want is to pass the clicked button value so that code can determine what it should do with the data

my button is

<div class="btn btn-success btn-sm multiple" value="delete">Delete</div>

2

Answers


  1. You can use attr for this:

    $('.multiple').on("click", function () {
        var action = $(this).attr('value');     
    
        console.log(action);       
    });
    

    Here is the demo

    Login or Signup to reply.
  2. $('.multiple').on("click", function () {
            var value = $(this).attr('value');     
                    alert(value);
            console.log(value);
           
        });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="btn btn-success btn-sm multiple" value="delete">Delete</div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search