16

I have been trying to add variables from a dropdown into an array using Jquery array.push() function but for some odd reason it is not working. Following is the jsFiddle link: http://jsfiddle.net/dKWnb/3/

JavaScript :

$("#test").live("click",function() {
       var myarray = new Array();
        myarray.push($("#drop").val());
        alert(myarray);
});

HTML

<Select name=drop id=drop>
    <option value=1>1</option>
    <option value=2>2</option>
</select>
<input type=button name=test id=test value=test>
kapa
  • 77,694
  • 21
  • 158
  • 175
Saad Bashir
  • 4,341
  • 8
  • 30
  • 60
  • What should it do? Because it does here exactly what it looks it should do. – netiul Dec 07 '11 at 10:09
  • 1
    Just a note: always declare arrays this way `var myarray = [];` – Didier Ghys Dec 07 '11 at 10:09
  • 6
    @Matt. Long story short, everything can be re-defined in javascript, so is `Array()`. Using `[]` guaranties you'll have an array. This is also more efficient. Check this [answer](http://stackoverflow.com/a/1273936/123127) that explains it pretty clearly. – Didier Ghys Dec 07 '11 at 10:18
  • 5
    For the record, the `.push()` method is native JavaScript and got nothing to do with jQuery. – Shadow The GPT Wizard Dec 07 '11 at 10:22

4 Answers4

32

Your HTML should include quotes for attributes : http://jsfiddle.net/dKWnb/4/

Not required when using a HTML5 doctype - thanks @bazmegakapa

You create the array each time and add a value to it ... its working as expected ?

Moving the array outside of the live() function works fine :

var myarray = []; // more efficient than new Array()
$("#test").live("click",function() {
        myarray.push($("#drop").val());
        alert(myarray);
});

http://jsfiddle.net/dKWnb/5/

Also note that in later versions of jQuery v1.7 -> the live() method is deprecated and replaced by the on() method.

Manse
  • 37,765
  • 10
  • 83
  • 108
7

Your code alerts the current value of the dropdown for me, showing that it has properly pushed into the array.

Are you wanting to keep old values and append? You're recreating the array each time, meaning that the old value gets clobbered.

Here's some updated code:

var myarray = [];
$("#test").click(function() {
    myarray.push($("#drop").val());
    alert(myarray);
});

jsFiddle

Interrobang
  • 16,984
  • 3
  • 55
  • 63
4

another workaround:

var myarray = [];
$("#test").click(function() {
    myarray[index]=$("#drop").val();
    alert(myarray);
});

i wanted to add all checked checkbox to array. so example, if .each is used:

var vpp = [];
var incr=0;
$('.prsn').each(function(idx) {
   if (this.checked) {
       var p=$('.pp').eq(idx).val();
       vpp[incr]=(p);
       incr++;
   }
});
//do what ever with vpp array;
sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42
-1
var array = [];
var element = "anything you want in the array";
array.push(element); // array = [ "anything you want in the array" ]
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 30 '21 at 10:22