Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:29:34+00:00 2026-05-29T11:29:34+00:00

I want to create a loop for input so that the variable img get

  • 0

I want to create a loop for input so that the variable img get number 1 to 5 like this:

img1, img2 ... img5.

How to write $i after img?

for ($i=1;$i<=5;$i++) {

  function(data) { $('input[name="img1"]').val(data) });

}

Note: img is between two quotation mark.

it’s edite:

user = $('input[name="name"]').val();
for (var i = 1; i <= 5; i++) {
  $.post("test.php", { name: user, num: i },
  function(data) {
    $('input[name="img'+i+'"]').val(data)
  });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:29:35+00:00Added an answer on May 29, 2026 at 11:29 am

    The function you have declared in your loop seems weird. That’s not valid javascript. You may try the following:

    for (var i = 1; i <= 5; i++) {
        $('input[name="img' + i + '"]').val(data);
    }
    

    or if we suppose that you have defined some function:

    var foo = function(data, index) {
        $('input[name="img' + index + '"]').val(data);
    }
    

    you could invoke it like this:

    for (var i = 1; i <= 5; i++) {
        foo('some data ' + i, i);
    }
    

    UPDATE:

    An interesting example was provided in the comments section:

    for (var i = 1; i <= 5; i++) { 
        $.post(
            "test.php", 
            { name: username, num: i }, 
            function(data) { 
                $('input[name="img'+i+'"]').val(data);
            }
        ); 
    } 
    

    This won’t work because the i variable might have changed value between the loop and the AJAX success callback. To fix this you may try the following:

    for (var i = 1; i <= 5; i++) { 
        (function(index) {
            $.post(
                "test.php", 
                { name: username, num: index }, 
                function(data) { 
                    $('input[name="img'+index+'"]').val(data);
                }
            ); 
        })(i);
    } 
    

    or use the $.ajax() method which allows you to pass a context to the success callback:

    for (var i = 1; i <= 5; i++) { 
        $.ajax({
            url: 'test.php',
            type: 'POST',
            data: { name: username, num: i },
            context: i, // here we are defining the context
            success: function(result) {
                // since we have used the context parameter, this variable
                // here will point to the value that i had when we initiated 
                // the AJAX request
                $('input[name="img' + this + '"]').val(result);
            }
        });
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a function like a for loop where I will ideally
I want to have a sweave document that will include a variable number of
I want to use nokogiri to loop through a html and create an object
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic
I am playing with Java and want to do a simple while loop that
Lets say that I want to create a list of names listOfNames = []
This is an ajax function that i would like carry out on click of
having a small problem. I'm trying to create a loop that will create an
i have a blob url like blob:blahblah that points to a file. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.