1

I am trying to fix a problem for hours and I just gave up trying to search & code & see if it works.

Maybe it will be better to explain with simple code what I am trying to do;

<script>
test1       = $('input:text[name=test1]');
test2       = $('input:text[name=test2]');
test3       = $('input:text[name=test3]');

var obj = [];
obj.push(test1);
obj.push(test2);
obj.push(test3);

$.each(obj, function(key, value) {
    console.log ('value : ' + value.val());
});

</script>
</head>

<body>
<input name="test1" type="text" value="1"/>

<input name="test2" type="text" value="2"/>

<input name="test3" type="text" value="3"/>
</body>
</html>

What I am trying to do is;

  1. Define variables which will be assign to DOM element.
  2. Create an array and put those variables which are assigned to DOM elements.
  3. Loop them and get their values etc...

Is there a way to do that? I am really tired after trying to make it work for hours.

Thank you for your help and time in advance.

EDIT:

Of course the code above wasn't something I was trying to achieve. I have a form which has over 700 lines of Jquery code and I had a problem in one of my functions.

The principle was the same so I decided to make something really short and clearly understandable. $value was just bad copy & paste. Before I realized I didn't delete all of my old code (just $ was left) and edited my post, some people already replied. It wasn't the correct answer but I just didn't want to close the question without giving points to anyone.

The code above will not work unless if you change it to;

$(document).ready(function(){
 // Put the code here
});

Considering my jquery code is in between <head> </head> I have to use $(document).ready

I hope this will help someone.

I am not newbie in Jquery, I don't know why and how I could make such mistake.

3
  • 2
    $value.val() should be value.val() Commented Sep 1, 2011 at 21:34
  • 1
    I just copied and pasted wrong. It was like this but for some reason I keep getting undefined... EDIT 2: Oh darn, I just realized. I put my code between <head></head> element without Document Ready... Commented Sep 1, 2011 at 21:36
  • 2
    Try removing the (key, value) and just declare it as function() and use this.val() Commented Sep 1, 2011 at 21:39

4 Answers 4

2

Try doing this:

$.each(obj, function() {
    console.log ('value : ' + this.val());
});

Demo

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

1 Comment

That's just more elegant (: Thank you for your reply.
1

You have an unnecessary $ in front of value in your each loop:

$value.val() should be value.val()

Working Demo

1 Comment

It was just bad copy & paste. As I wrote comment for my question, the real reason was; $(document).ready. My code was in <head></head> element. Anyway thank you for your reply.
1

You are on the right track; instead of using $value.val() use $(this). Like this:

$.each(obj, function() {
    console.log ($(this).val());
});

Comments

-1

maybe?

var obj = [$('input:text[name=test1]'),
           $('input:text[name=test2]'),
           $('input:text[name=test3]')];

$.each(obj, function(key, value) {
    console.log ('value : ' + value.val());
});

Comments

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.