Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  29] [ 3]  / answers: 1 / hits: 159870  / 13 Years ago, wed, december 7, 2011, 12:00:00

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>

More From » jquery

 Answers
6

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.


[#88702] Tuesday, December 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jillalanisg

Total Points: 484
Total Questions: 98
Total Answers: 89

Location: Vanuatu
Member since Wed, Oct 14, 2020
4 Years ago
;