0

Here is where I set up my var adultOptions and in this var you will see name="adult'+i+'Name" /> I want that i to be the loop number you see below.

Any ideas?! Thank you

var adultOptions =
'<input type="text"'+
'placeholder="First &amp; Last Name"'+
'required name="adult'+i+'Name"/>'


$('#adults').on('change',function(i){
    numberAppend = $('#adults').val();
    for(i=0; i<numberAppend; i++){
    $(adultOptions).appendTo('#adultOptions');
  }    
});

2 Answers 2

4

You are nearly correct with your method. What you are lacking is, maybe the knowledge that a function can be stored in a variable in javascript.

So, now with this knowledge, you can make adultOptions point to a function, like this:

var adultOptions = function(i) {
return '<input type="text"'+
'placeholder="First &amp; Last Name"'+
'required name="adult'+i+'Name"/>';
}

and, then use it like this:

$('#adults').on('change',function(i){
    numberAppend = $('#adults').val();
    for(i=0; i<numberAppend; i++){
    $(adultOptions(i)).appendTo('#adultOptions');
  }    
});
Sign up to request clarification or add additional context in comments.

1 Comment

Stoic, your absolutely right thank you! One of those nights when I am a little too close to what I'm working on. Thank you
1

Your string is being defined outside of your for loop (where i is being defined). Put it inside and you should be good.

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.