19

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
78.9k21 gold badges166 silver badges178 bronze badges
asked Dec 7, 2011 at 10:06
4
  • What should it do? Because it does here exactly what it looks it should do. Commented Dec 7, 2011 at 10:09
  • 1
    Just a note: always declare arrays this way var myarray = []; Commented Dec 7, 2011 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 that explains it pretty clearly. Commented Dec 7, 2011 at 10:18
  • 5
    For the record, the .push() method is native JavaScript and got nothing to do with jQuery. Commented Dec 7, 2011 at 10:22

4 Answers 4

33

(削除) 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.

answered Dec 7, 2011 at 10:09
Sign up to request clarification or add additional context in comments.

2 Comments

He uses an HTML5 doctype. It is not obligatory to use quotes around attribute values.
@bazmegakapa ok understood - i have learnt something there - answer updated
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

answered Dec 7, 2011 at 10:09

Comments

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;
answered Jun 18, 2019 at 5:33

Comments

-1
var array = [];
var element = "anything you want in the array";
array.push(element); // array = [ "anything you want in the array" ]
answered Dec 30, 2021 at 10:17

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.