0
<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 !!!!!

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I want that array result outside the function
It is outside of the function as you can see from the code its self. Like i said in other comments, this array can be accessed from anywhere. Therefore this is exactly what you want.
1

Try this

$( document ).ready(function() {
    var arr=new Array();
    $('.sal').each(function() {
        $(this).click(function(i) {
            arr.push($(this).val());
        }); //missing ); here!
    });
});

Comments

1

You can create array to store values of all clicked buttons:

Fiddle: https://jsfiddle.net/1tbLbmkg/

    var values = []; // declare it outside document.ready
    $( document ).ready(function() {   
        $('.sal').click(function(i) {
             values.push($(this).val()); // add value to array
        }); 
    }); 

3 Comments

but then I am not able to access values in values array when I finished with clicks (console.log(values);)
Yes you can access them, the values array defined in this code is global it can be accessed from anywhere.
check fiddle...in that you can get array
1

You can simplifyy this function. note that since all inputs contain 0 as their value- you will end up with a bunch of zeroes in the aray. You also have to declare the empty array before pushing the value into it. Fiddle:https://jsfiddle.net/gavgrif/xL7qsbsn/

$( document ).ready(function() {
            var newArray= new Array();
            $('.sal').click(function() {
                 newArray.push($(this).val());
                  alert("newArray contents = "+ newArray)
            });
       });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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">

5 Comments

newArray[] is not valid JavaScript.
I have been taught that it is - that [] the equivalent of new array(), but I will change it for you.
Your code is not working at all..throwing jquery error: jsfiddle.net/03qjqags
I meant newArray[] = $(this).val();. As far as I'm aware it works on php, but on JavaScript it should be newArray.push($(this).val());
sorry - answer amended and test it works now. Thanks for the fiddle @Dhara Parmar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.