Add to HTML form without losing current form input information in Javascript
Asked Answered
S

6

9

I have a drop down which builds a form based of the selections that are selected. So, if someone selects 'foobar', it displays a text field, if they choose 'cheese', it displays radio buttons. The user can then enter data into these forms as they go along. The only problem is that when they add a new form element, all the rest of the information is erased. Im currently using the following to do add to the form:

document.getElementById('theform_div').innerHTML = 
    document.getElementById('theform_div').innerHTML + 'this is the new stuff';

How can I get it to keep whatever has be enetered in the form and also add the new field to the end?

Seafaring answered 3/2, 2010 at 14:51 Comment(0)
E
16

Setting innerHTML destroys the contents of the element and rebuilds it from the HTML.

You need to build a separate DOM tree and add it by calling appendChild.

For example:

var container = document.createElement("div");
container.innerHTML = "...";
document.getElementById("theform_div").appendChild(container);   

This is much easier to do using jQuery.

Erma answered 3/2, 2010 at 14:53 Comment(0)
S
5

Step One:

Add jQuery to your headers:

<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”></script>

Step Two:

Append, don't replace, data to your DIV like this:

$("#theform_div").append("your_new_html_goes_here");
Stedfast answered 3/2, 2010 at 14:56 Comment(1)
this worked for me, I had to add nested elements (<li..><input..> inside an <ul>)Gregale
T
2

Don't use innerHTML to create the form elements. With innerHTML you're overwriting the old HTML with new HTML which will recreate all the elements. Instead you need to use the DOM to create and append the elements.

EXAMPLE

function addRadioElement()
{
    var frm = document.getElementById("form_container");
    var newEl = document.createElement("input");
    newEl.type = "radio";
    newEl.name = "foo";
    newEl.value = "bar";
    frm.appendChild(newEl);        
}
Tetralogy answered 3/2, 2010 at 14:53 Comment(0)
B
2

The most correct way to do it without using a framework (like jQuery, Dojo, YUI) is:

var text = document.createTextNode('The text you want to write');
var div = document.createElement('div');
div.appendChild(text);

document.getElementById('theform_div').appendChild(div);

innerHTML, although supported by most browsers, is not standard compliant and - therefore, not guaranteed to work.

Blackguardly answered 3/2, 2010 at 14:56 Comment(0)
T
0

I would suggest using jQuery and its append function.

Travelled answered 3/2, 2010 at 14:52 Comment(0)
B
-1

If you want to generate the element using an HTML string, as innerHTML does, a great option is the insertAdjacentHTML function, with it's position parameter:

let newElement = "<div>this is the new stuff</div>";
document.getElementById("theform_div")
.insertAdjacentHTML("beforeend", newElement);

As you can see from Mozilla's documentation, besides being faster, this solves the problem you had because the function doesn't reparse the parent element:

The insertAdjacentHTML() method does not reparse the element it is being used on, and thus it does not corrupt the existing elements inside that element. This avoids the extra step of serialization, making it much faster than direct innerHTML manipulation.

However, if you just want to add plain text, not HTML code, then there is a similar function made just for that called insertAdjacentText:

let newText = "this is the new stuff";
document.getElementById("theform_div")
.insertAdjacentText("beforeend", newText);
Bloody answered 1/6, 2023 at 21:3 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Jeannajeanne

© 2022 - 2024 — McMap. All rights reserved.