0

I have a jquery as below:

$('.cl').click(function(){
var selected_locations;
var loc_id = $('.a').val();
alert(loc_id);
selected_locations.push(loc_id);
alert(selected_locations);
});

And the HTML :

<input type="text" name="a" class="a" value="1" />
<a href="#" class="cl">Click</a>

But when I click on the link , though it displays the value of loc_id, it does not alerts thereafter, ie the value of the array selected_locations . Whats wrong ? DEMO HERE

Nitish
  • 2,695
  • 9
  • 53
  • 88

3 Answers3

7

You forgot to initialize it as a list. It is undefined by default;

var selected_locations = [];
The Internet
  • 7,959
  • 10
  • 54
  • 89
2

Initialize your array

var selected_locations = [];

and

alert(selected_locations.join());

fiddle Demo

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
2

Declare a JavaScript array

var selected_locations = [];

or

var selected_locations = new Array();
Community
  • 1
  • 1
Prateek
  • 6,785
  • 2
  • 24
  • 37