2

I have a javascript function which adds a div with three inputs to the page and a link to add more inputs to the same div (Imagine that user doesn't know how many fields there will be so he can add them dynamically).

var div=document.createElement("div");
div.innerHTML='<input type="text" name="question" /><br><br>'+
              '<input type="text" name="input1"/><br />'+
              '<input type="text" name="input2"/><br />'+
              '<a href="javascript:addInput()">Add input field</a>';

var parent_div=document.getElementById('dinamicni_div');
parent_div.appendChild(div);

How could I now implement this addInput() function to add an input below the last input (currently input2) in the same div (but above the link)?

Just to make clear - I have two JS functions to generate content dynamically: addDiv(), which you can see above shortened out; and addInput() which I would like to implement and it should add one input in the current div, where the link was clicked.

2
  • Could we see the function you're trying to improve? Commented Apr 21, 2012 at 19:48
  • It's not implemented yet, but now that I've gotten the tips I'm off to implementing it :) Commented Apr 21, 2012 at 19:56

4 Answers 4

5

Try

document.getElementById('yourdiveID').innerHTML += "<input type='text' value='' /><br />";

It will add at the end of the div.

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

1 Comment

This method will add input AFTER the link and I want it before - except if the inputs are in their own div. Thanks
3

give your link a class or id and insert using this method: http://api.jquery.com/insertBefore/

edit:

i agree, the question was not about jquery, yet the asker hinted, that his requirements would allow for it, and for completeness sake, here is the code in jquery (to take the posters unjustified intimidation of learning jquery)

function createInputMethod(){
    return $('<input type="text" name="question" /><br><br>'+
              '<input type="text" name="input1"/><br />'+
              '<input type="text" name="input2"/><br />');
}

var div = $("<div/>");
div.append(createInputMethod());
div.append($('<a href="javascript:addInput()" id="btn">Add input field</a>');
var parent_div=$('dinamicni_div');
div.appendTo(parent_div);

function addInput(){
   createInputMethod().insertBefore("#btn");
}

3 Comments

Do you see jQuery mentioned at all in the question?
It is jQuery indeed but still thanks for mentioning it, it looks nice and I should learn jQuery :)
@AndrewWhitaker, no, but i still liked the odds
1

You could add a div that contains the input fields inside the div you created, then it would be easy to add new elements to it by appending childs as you did before.

3 Comments

Thanks, this would be the most elegant answer from my point of view. I'll use either this or jQuery solution.
You can combine the layout and jQuery to make it simple and elegant :)
Well using jQuery won't be as simple as I worked with it briefly a couple of years ago :)
1

I would do something like this. Get all the inputs. Count number of inputs and add using jquery after method. Code is something like this:

function addInputMethod() {

    var len = $('div').find('input').length;


    $('input:nth-child(' + len + ')').after('<input type="text" name="input' + (len + 1) +'"/><br />'); 
}

EDIT

If you do not have an id for div you can change length to this

var len = $(this).parent().find('input').length;

Also change the second line to

$(this).parent().find('input:nth-child...

4 Comments

One question though - since I have many of these div's containing these inputs, how could I know from which one of them was I redirected to function (in which the link was clicked)?
Anji, I think something is wrong with the first line (of your update). It causes an error. Are you sure it's OK?
@PrimožKralj edited. I had an extra parenthesis. Thanks for finding it.
Thank you, you've helped alot.

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.