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

asked Dec 18, 2013 at 9:39
1
  • 2
    you can push anything in undefined variable ;) Commented Dec 18, 2013 at 9:43

3 Answers 3

7

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

var selected_locations = [];
answered Dec 18, 2013 at 9:40
Sign up to request clarification or add additional context in comments.

Comments

2

Initialize your array

var selected_locations = [];

and

alert(selected_locations.join());

fiddle Demo

answered Dec 18, 2013 at 9:41

Comments

2

Declare a JavaScript array

var selected_locations = [];

or

var selected_locations = new Array();
answered Dec 18, 2013 at 9:43

Comments

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.