3

I need to Loop in JQuery from 0 to variable-value(dynamically entered by user).How can i achieve this? Now i am doing it by using simple For loop like this.

for( i=1; i<=fetch; i++) {  
   var dyndivtext = document.createElement("input");
   document.body.appendChild(dyndivtext); 
}

Thanks.

8
  • How does users entered the variable? Commented Aug 24, 2012 at 8:08
  • You could still use foo loop in jQuery. Commented Aug 24, 2012 at 8:09
  • 2
    What's wrong with your current code? Commented Aug 24, 2012 at 8:10
  • It is working in javascript for loop now.i need to do it in JQuery using .each loop. Commented Aug 24, 2012 at 8:15
  • @evanc3 User enter the variable in a text box that i am retrieving in a variable named "fetch". Commented Aug 24, 2012 at 8:16

5 Answers 5

2

To loop between two values you should use a regular Javascript loop. The jQuery each methods are used when looping through a collection of elements or an array.

To loop from zero, you should initialise the loop variable to zero, not one. To loop from zero to the specified value, you use the <= for the comparison, but to loop from zero and the number of items as specified (i.e. from 0 to value-1), you use the < operator.

for (i = 0; i < fetch; i++) {
  $('body').append($('<input/>', { type: 'text' }));
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user853575: Yes, and you should keep doing that. If you want to use each for the loop, you first need a for loop to create an array that you can loop through, so that is completely pointless.
2

You could loop an empty array:

$.each(new Array(fetch), function(i) { 
   var dyndivtext = document.createElement("input");
   document.body.appendChild(dyndivtext); 
});

If you do this alot you can even fake-patch jQuery.each to take numbers:

(function($) {
    var _each = $.each;
    $.each = function() {
        var args = $.makeArray(arguments);
        if ( args.length == 2 && typeof args[0] == 'number') {
            return _each.call(this, new Array(args[0]), args[1]);
        }
        return _each.call(this, args);
    };
}(jQuery));​

$.each(fetch, function(i) {
    // loop
});

jQuery.each does have some great features, like the different return values inside the callback. But for a simple loop I find it much more convenient (and less overhead) to do something like:

while(fetch--) {
    // loop
}​

Comments

1

You mean Javascript loop. From W3Schools:

for (var variable = startvalue; variable < endvalue; variable = variable + increment)
{
      //code to be executed
}

Comments

0

To get the value from user and run the code you can use the following prompt.

var x=prompt("Enter the value",0);
for(i=0;i<x;i++)
{
  var dyndivtext = document.createElement("input");
  document.body.appendChild(dyndivtext);
}

Hope this helps.

Thanks

3 Comments

To use the .each() you need to have a collection of elements. As per my believe you cannot use the .each() to run with the requirement you have. Also note that performance wise for loop is better than .each() function as well.
Not strictly a collection of elements it will work with arrays and some other objects but it wont work with an number as you say. +1
Yes, true. I mentioned the arrays also as collection.
0

If you want it the full jQuery way then use that new plugin jQuery-timing. It provides inline-loops in your jQuery line:

$('body').repeat().append('<input>').until(fetch);

Nice, eh?

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.