1

I want to add a box with individual boxes inside it with every age when the function is run. I tried doing it by splitting the innerHTML and using the for loop on just the agebox section so it will loop and create a new age box each time and not create a whole outerbox as well everytime like if you try loop the entire thing. I thought this would work but now it creates an age box for each loop but its placed outside the outer box and i cant figure out how to get it to loop within the outer box. If i remove the loop and just create one innerHTML then the age boxes i made manually are inside the outer box so im assuming theres a problem with the actual splitting up of the innerHTML. Thanks in advance!!

function Age(gender){
        if (gender!==undefined){
            el1 = document.getElementById('userdata');
            el1.innerHTML += gender +"<br>";    
        }
        el1 = document.getElementById('farespage');
        el1.innerHTML += "<div id=\"outerbox\">";
        for(var i=13; i<=18; i++){
            el1.innerHTML +="<div class=\"agebox\" onclick=\"Relationship('"+i+"')\">"+i+"</div>";
        }
        el1.innerHTML += "</div><button type=\"button\" onclick=\"goback('Gender')\">back</button>";
    }
1
  • 1
    Just don't treat the DOM as though it's a string of HTML, and you'll do much better. Commented Apr 20, 2015 at 21:24

2 Answers 2

4

You need to store the output content as a string and then append it to the DOM. Otherwise, the div will be auto-closed.

el1 = document.getElementById('farespage');

output = "<div id=\"outerbox\">"; //initialize output string

//build output string
for(var i=13; i<=18; i++){
    output +="<div class=\"agebox\" onclick=\"Relationship('"+i+"')\">"+i+"</div>";
}

output += "</div><button type=\"button\" onclick=\"goback('Gender')\">back</button>";

el1.innerHTML = output; //output to DOM

View Fiddle

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

5 Comments

That makes a lot of sense but when i try it then nothing happens at all now
It works in the fiddle, so there must be something else going wrong in your code.
Ah i changed the id and class names when i wrote this so it would be easier to explain and i just copy pasted your code haha but now its working perfectly. Thanks!!
Nice solution. Works fine.
I needed to generate 1 to 5 input fields using a range input to generate the numeric value and this example give me the answer I needed to make it work. Thanks a lot.
1

The line

el1.innerHTML += "<div id=\"outerbox\">";

is actually producing

<div id="outerbox"></div>

because most browsers will auto-close the HTML tags.

You should write all your HTML into a string buffer then append it with one big call; for example:

function Age(gender){
    if (gender!==undefined){
        el1 = document.getElementById('userdata');
        el1.innerHTML += gender +"<br>";    
    }

    el1 = document.getElementById('farespage');
    // Magic begins here
    var yourHTML = "";
    yourHTML += "<div id=\"outerbox\">";
    for(var i=13; i<=18; i++){
        yourHTML +="<div class=\"agebox\" onclick=\"Relationship('"+i+"')\">"+i+"</div>";
    }
    yourHTML += "</div><button type=\"button\" onclick=\"goback('Gender')\">back</button>";

    el1.innerHTML += yourHTML;
}

This has the added benefit of only touching the DOM once and not 7 times (which is generally a good thing).

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.