1

I'm trying to create an array of textarea values and then loop through them.

Here is a jsFiddle that I am trying to get this to work in:

http://jsfiddle.net/kxkHZ/1/

When I run it, nothing happens.

Any idea what I'm doing wrong?

var textArray = [];
$('[class=objectives]').each(function (i) {
     textArray.push($(this).val());
});

for (var i = 0; i < textArray.length; i++) {
     console.log(textArray[i].value);
}
1
  • 1
    Just change to console.log(textArray[i]); Commented May 21, 2013 at 15:04

3 Answers 3

5

There is no value property in each element, you can simply do:

for (var i = 0; i < textArray.length; i++) {
     console.log(textArray[i]);
}

Working Demo - Note: jQuery wasn't originally included

Sign up to request clarification or add additional context in comments.

4 Comments

There is a good practise to store array length in a variable, and then use it in for loop
You mean for (var i=0, n=textArray.length; i<n; i++) ?
for some reason it doesn't show up in my firebug console when I run it in jsfiddle...
@999cm999 - jsfiddle.net/kxkHZ/7 <- This does print four strings in the console.
4

You are pushing the value of the element into the array, you do not need to call value on it again. Just access the string itself:

console.log(textArray[i]);

Comments

3

Working --> http://jsfiddle.net/kxkHZ/11/

for (var i = 0; i < textArray.length; i++) {
    // textArray[i] itself is the value so textArray[i].value is incorrect 
    console.log(textArray[i]); 
}

You forgot to include jquery -

You should have got an error $ is not defined.

1 Comment

Still doesn't work. You wind up writing 'undefined' to the console 4 times.

Your Answer

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.