1

I am looking to loop through all inputs on my page and declare a variable for each using the the id as name. This is what i have tried without success

$('input').each(function(){
    eval( 'var'+$(this).attr('id')+'='+$(this).val());
});
4
  • 1
    why do you want those variables.... why can't you use an object Commented Nov 14, 2013 at 16:16
  • I am feeding the results into an existing script Commented Nov 14, 2013 at 16:17
  • @DouglasCottrell use JSON object, so you can use them in your code Commented Nov 14, 2013 at 16:18
  • 2
    output the string you're generating inside eval and you'll see: you're missing a space after var and quatation marks aroudn the value. Commented Nov 14, 2013 at 16:19

3 Answers 3

2

Try this see if this works :

   $('input').each(function(){
        window[this.id] = this.value;
    });
Sign up to request clarification or add additional context in comments.

3 Comments

If you must do this, please do window[this.id] = this.value;.
@JohannesH. More to the point, even if we are using jQuery (and it isn't inherently a bad thing), it's still always a bad idea to do $(this).attr('id').
@lonesomeday I DO consider using jQuery for tasks that are not any more complex without it a bad thing. It's awfully slow - see test case linked in my answer. But you sill got a point of course ;)
1
$('input').each(function(){ 
   window[this.id]=this.value;
});

Since all variables are object of window, you can initiate variables like this

1 Comment

+1 for not using jQuery for getting the ID. But there is still no reason to use it for the loop...
0

Here is a solution without ANY jQuery. Plain old Javascript. Way faster, easier to read, no drawbacks. Jeez, why did all people start learning jQuery before they even got the basics?

var inputs = document.getElementsByTagName('input');
for (var i = 0 ; i < inputs.length ; i++ ) {
    window[inputs[i].id] = inputs[i].value;
}

In recent versions of all majot browsers, you can do the same thing using querySelectorAll(). It is more flexible, but almost as slow as jQuery.

var inputs = document.querySelectorAll('input');
for (var i = 0 ; i < inputs.length ; i++ ) {
    window[inputs[i].id] = inputs[i].value;
}

4 Comments

That's an interesting performance comparison, though in practical terms not overwhelming, in my opinion (unless we're doing a lot of tests on a lot of data). But I have to say, I disagree that your loops are easier to read! If I were doing this in my own code (which I wouldn't, because it's inherently stupid) I would use jQuery for the selection and loop in almost all circumstances, providing it was already loaded.
@lonesomeday If you don't like the loop, use developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… - it almost looks like jQUery afterwards (only the function names are a bit longer). document.getELementsByTagName('input').forEach(function (element) { /*...*/ }); DOn'T get me wrong, I do use jQUery myself, especiall when dealing with AJAX. SO if it's loaded anyway, it's ok to be lazy and us it for simple tasks. But when I see people using jQuery for literally everything, I always get curious if they even know plain JS.
That won't work, though, because getElementsByTagName doesn't return an array. But you're right that learning how to do things the DOM way is always good for JS developers, even if they always use jQuery.
Oh. Yes, you're right, NodeLists are not arrays. Sorry, my bad!

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.