Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-18-2012, 11:31 AM   PM User | #1
pixo_newb
New Coder

 
Join Date: Nov 2011
Posts: 28
Thanks: 13
Thanked 0 Times in 0 Posts
pixo_newb is an unknown quantity at this point
Wrapping javascript loop in a div

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:

Code:
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>";
	
		for(z=0;z<nTSplit[a].length;z++){
			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.
pixo_newb is offline   Reply With Quote
Old 06-18-2012, 12:07 PM   PM User | #2
pixo_newb
New Coder

 
Join Date: Nov 2011
Posts: 28
Thanks: 13
Thanked 0 Times in 0 Posts
pixo_newb is an unknown quantity at this point
Wow, create the separate divs in the original for loop (a). Don't know how I missed that.

Feel free to delete the thread.
pixo_newb is offline   Reply With Quote
Old 06-18-2012, 08:32 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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?
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 06-19-2012, 01:22 PM   PM User | #4
pixo_newb
New Coder

 
Join Date: Nov 2011
Posts: 28
Thanks: 13
Thanked 0 Times in 0 Posts
pixo_newb is an unknown quantity at this point
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?
pixo_newb is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:28 PM.


Advertisement
Log in to turn off these ads.