Skip to content Skip to sidebar Skip to footer

Using Javascript To Add Form Fields.. But Below, Not To The Side?

So as I click the button, the javascript adds new fields. Currently it adds the new text box to the side.. is there a way to make it add below? I guess as if there were a . Here is

Solution 1:

Insert a <br/> tag infront of the inserted input or better yet, put the input into a div and control the look of it with CSS.

Solution 2:

Add this to the end of your function:

document.body.insertBefore(document.createElement("br"), element);

Full code:

<html><head><scripttype="text/javascript">var instance = 1;

                functionnewTextBox(element)
                {               
                        instance++; 
                        var newInput = document.createElement("INPUT");
                        newInput.id = "text" + instance;
                        newInput.name = "text" + instance;
                        newInput.type = "text";
                        //document.body.write("<br>");document.body.insertBefore(newInput, element);

                        document.body.insertBefore(document.createElement("br"), element);
                }
        </script></head><body><inputid="text2"type="text"name="text1"/><br><inputtype="button"id="btnAdd"value="New text box"onclick="newTextBox(this);" /></body></html>

Solution 3:

Just create a <br> element the same way and put it between.

var newBr = document.createElement("BR");
document.body.insertBefore(newBr, element);

Or use CSS. The display:blockmay be of value.

Solution 4:

You could either, insert br element after the new input, or wrap it inside a div element:

functionnewTextBox(element) {                
    instance++; 
    var newInput = document.createElement("INPUT"); 
    newInput.id = "text" + instance; 
    newInput.name = "text" + instance; 
    newInput.type = "text"; 

    var div = document.createElement('div'); 
    div.appendChild(newInput); 
    document.body.insertBefore(div, element); 
} 

Check the above example here.

Post a Comment for "Using Javascript To Add Form Fields.. But Below, Not To The Side?"