Search code examples
javascripthtmlformsdomappendchild

HTML DOM - element.appendChild() doesn't behave as I expect it to


link to code (js and css can be found through the page source): https://dl.dropboxusercontent.com/u/16952797/webdev/uppg1/kontakt.html alt link: http://jsfiddle.net/DdQhk/ (although jsfiddle does not render the page properly)

relevant lines of code (function createFormBug() - line 31 to 66 in kontakt.js) code:

function createFormBug() {
    var form = document.createElement("form");
    var fieldset = document.createElement("fieldset");
    var legend = document.createElement("legend");
    var labelFunction = document.createElement("label");
    var labelInterface = document.createElement("label");
    var labelComment = document.createElement("label");
    var radioFunction = document.createElement("input");
    radioFunction.type = "radio";
    var radioInterface = document.createElement("input");
    radioInterface.type = "radio";
    var textarea = document.createElement("textarea");
    var buttonSubmit = document.createElement("input");
    radioInterface.type = "submit";

    form.id = "formBug";

    legend.textContent = "Bugg";

    document.getElementById("divForm").appendChild(form);

    form.appendChild(fieldset);
    fieldset.appendChild(legend);
    fieldset.appendChild(labelFunction);
    fieldset.appendChild(radioFunction);
    fieldset.appendChild(labelInterface);
    fieldset.appendChild(radioInterface);
    fieldset.appendChild(labelComment);
    fieldset.appendChild(textarea);
    fieldset.appendChild(buttonSubmit);





}

context: I'm trying to create a form dynamically by using js, unfortunately some elements aren't being appended for some reason.

partial output of generated html when run on Chrome (relevant section):

<div id="divForm">
    <form id="formBug">
        <fieldset>
            <legend>Bugg</legend>
            <label></label>
            <input type="radio">
            <label></label>
            <input type="submit">
            <label></label>
            <textarea></textarea>
            <input>
        </fieldset>
    </form>
</div>

Solution

  • I think your code is working pretty much as you might expect it to. The only obvious mistake is that you are setting radioInterface.type = "submit"; where you presumably mean buttonSubmit.type = "submit";.

    It should be apparent that this works fine if you actually put some content in to make elements like label actually visible...

    Something like this, perhaps?

    var form = document.createElement("form");
    var fieldset = document.createElement("fieldset");
    var legend = document.createElement("legend");
    
    var labelFunction = document.createElement("label");
    labelFunction.textContent = 'Function'; // <-- added
    var labelInterface = document.createElement("label");
    labelInterface.textContent = 'Interface'; // <-- added
    var labelComment = document.createElement("label");
    labelComment.textContent = 'Comment'; // <-- added
    
    var radioFunction = document.createElement("input");
    radioFunction.type = "radio";
    var radioInterface = document.createElement("input");
    radioInterface.type = "radio";
    
    var textarea = document.createElement("textarea");
    
    var buttonSubmit = document.createElement("input");
    buttonSubmit.type = "submit"; // <-- corrected
    

    I believe your code to actually add them to the document should be working fine.