arpit Pathak arpit Pathak - 6 months ago 20
PHP Question

on each button click store value in array

<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">


On each button click store clicked value in a array
I have tried this

$( document ).ready(function() {
$('.sal').each(function() {
$(this).click(function(i) {
var i[]=$(this).val();
console.log(i);
});
});
});


What I am missing here !!!!!

Answer

You can try this code with your html:

var i = [];

    $( document ).ready(function() {        
        $('.sal').each(function() {
                $(this).click(function(e) {        
                     i.push($(this).val());
                    console.log(i);
                }); 
         });
    });

JSFiddle

What is wrong with your code? first of all you are not initializing the i array properly. also you need to define it outside of the scope of the click function because you are going to loose it after the function is done. If you do not want to pollute the global scope you can define the array inside the function of the ready() method.