Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    28
    Thanks
    13
    Thanked 0 Times in 0 Posts

    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.

  • #2
    New Coder
    Join Date
    Nov 2011
    Posts
    28
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Wow, create the separate divs in the original for loop (a). Don't know how I missed that.

    Feel free to delete the thread.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,610
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Nov 2011
    Posts
    28
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •