View Full Version : Wrapping javascript loop in a div

06-18-2012, 12:31 PM
Hi, I don't understand how this works. If I'm writing JS code in the head of the document, and I want to wrap a chunk of it within a CSS div, how do I do this?

For instance:

for (a=0;a<req_Links;a++){
e$("menu_Space").innerHTML = e$("menu_Space").innerHTML + "<div class='parent" + a + "'>" + e$("link_Name" + a).value + "</div>";

e$("menu_Space").innerHTML = e$("menu_Space").innerHTML + "<div class='nest" + a + "'>" + nTSplit[a][z] + "</div>";

I want to wrap each run of the loop (technical term?) in a separate div. So the first time it runs I want to name the output #example_id0, then #example_id1 etc, and so on.

So if my chunk of code runs 3 times, I want 3 separate divs for each output.

e$ = document.getElementById btw.

I thought I could just write in the start of the loop in document.getElementById("menu_Space").innerHTML, and close it AFTER the code in the same way, but this doesn't work. I'm just a bit confused.

Any help is greatly appreciated.

06-18-2012, 01:07 PM
Wow, create the separate divs in the original for loop (a). Don't know how I missed that.

Feel free to delete the thread.

06-18-2012, 09:32 PM
Why is your JavaScript in the head of the document rather than at the bottom of the body where JavaScript that needs to interact with the page belongs?

06-19-2012, 02:22 PM
No idea.

I've read to basically always place the JS in the head. Will there be a discernible difference between placing it in the body as opposed to the head?

'It is a common practice to put all functions in the head section, or at the bottom of the page. This way they are all in one place and do not interfere with page content.'

Placing it in the bottom means it won't run before the elements it is trying to interact with are created? Is this accurate?