0

I'm trying to store in array using onclick on checkbox input code is shown below :

function CheckBoxShortList(x,y,z)
    {
        var slide = '';
        var arr = [];
        arr.push = x + ',' + y + ',' + z;    
        console.log(arr);
    }

CheckBox code is below :

<input type="checkbox" onclick="CheckBoxShortList('@item.Cid','@item.Hid','@k');">

It shows current value on console.log(arr) which is okay but if i use console.log(arr.length) then it is 0 not incrementing. Also i want to remove from array as i uncheck the input box.*

2
  • move ``var arr = []` outside of your function Commented Aug 1, 2017 at 7:55
  • check my answer Commented Aug 1, 2017 at 8:07

5 Answers 5

1

Array.prototype.push is a method. You have to call it, instead of assigning a value to it:

function CheckBoxShortList(x,y,z) {
    var slide = '';
    var arr = [];
    arr.push(x + ',' + y + ',' + z); // length is 1
    console.log(arr);
    console.log('Length:', arr.length);
}
<input type="checkbox" onclick="CheckBoxShortList('a','b','c');">

This results in an array containing one string: ["a, b, c"].

If you want to push the values one by one, do:

arr.push(x); // length is 1
arr.push(y); // length is 2
arr.push(z); // length is 3

This results in an array containing three values: ["a", "b", "c"].

For removing it, use Array.prototype.pop:

arr.push(x); // length is 1
arr.push(y); // length is 2
arr.push(z); // length is 3
arr.pop();   // length is 2
arr.pop();   // length is 1
Sign up to request clarification or add additional context in comments.

4 Comments

can you give me a example sir
What should the result look like?
that is showing but can i see its count and length which is returning 0.
actually its like i want to insert inside array onclick checkbox then pass the array to server.
0

You can actually just use Array's slice function on an arguments object, and it will convert it into a standard JavaScript array

function CheckBoxShortList() {

    var arr = []; // whatever your array

    var args = Array.prototype.slice.call(arguments); // array

    arr.push(args.join(','));    // string you push

    console.log(arr); // show o/p

    console.log(arr.length); // it shows 1 because you are pushing only one element to array
}
<input type="checkbox" onclick="CheckBoxShortList('a','b','c');">

2 Comments

Sir that is true but my input checkbox is dynamic upto 30 list i want to add these 30 in the array.
@RajatShetty doesn't matter even if you input CheckBoxShortList('a','b','c',1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30); like this it will join each element with comma
0

Earlier you were storing your values in an object named arr in its field named push.
Considering your values to be stored as a string in an array we call upon push method on it.

var arr = [];
function CheckBoxShortList(x,y,z)
    {
        var slide = '';

        arr.push(x + ',' + y + ',' + z);    
        console.log(arr);
    }

Considering your values to be stored separately

var arr = [];
function CheckBoxShortList(x,y,z)
    {
        var slide = '';

        arr.push(x);
        arr.push(y);
        arr.push(z);
        console.log(arr);
    }

2 Comments

that is true if on single call it will store those three values but after i click again then what old values are gone new are created over the array.
@RajatShetty Do you want to flush out prev values?
0

define you array outside the function and fire your event onchange() instead of onclick()

 
    
    
    var arr = [];
    function CheckBoxShortList(x,y,z) {
        if($('#test').is(":checked")){
          var slide = '';
          arr.push(x + ',' + y + ',' + z);    
          console.log(arr);
        }else{
          arr.pop(x + ',' + y + ',' + z);
          console.log(arr);
        }
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="test" type="checkbox" onchange="CheckBoxShortList('@item.Cid','@item.Hid','@k');">

Comments

0

As PeterMader said, Array.prototype.push is a method, it has to be passed the argument like this:

arr.push(x + ',' + y + ',' + z); 

If you wish to know why your code did not generate an error before it is because arrays are (obviously) objects in JavaScript. So when you says array.attr = value, the array object gets assigned an attribute attr whose value is value. In your case the array was assigned an attribute called push that stored your values, however, the array itself as a list was still empty which is why the array length was zero since additional attributes do not change the length of the array.

If you wish to add the items separately, you can use .push() for each item alone. You can also add another array to the original array using arr.contact(otherArray).

Hope that helps a little.

3 Comments

Sir my input checkbox is dynamic say 30 are these in the list i want to add 30 of these in the array like this method "arr.push(x + ',' + y + ',' + z);"
Hi Rajat, I updated my answer. You can push each item alone. Conversely, if you have several items in a different array you can use concat() to add the array to arr. Again let me know if I can be of more help to you.
Thank you Y2H i'll try this method

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.