Results 1 to 7 of 7

Thread: Dynamically adding a div.

  1. #1
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Dynamically adding a div.

    How do I add a div dynamically That i can alter afterwards.

    Code:
    function addElement(parent, id, style, inner) {
      var parentElement = document.getElementById(parent);
      var newdiv = document.createElement('div');
      newdiv.setAttribute('id',id);
     newdiv.innerHTML = inner;
      parentElement.appendChild(newdiv);
      return true;
    }
    this adds it but I can't alter the style. Any ideas what I'm doing wrong?


  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function addDiv(parent, id, style, children) {
      var r = document.createElement("div");
      if(id)
        r.id = id;
      if(style)
        for(var x in style)
          r.style[x] = style[x];
      if(children)
        for(var i = 0; i < children.length; ++i)
          r.appendChild(children);
      parent.appendChild(r);
      return r;
    }
    Stay away from innerHTML.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Wicked... but

    in the function addDiv
    parent = object?
    id = string
    style = string?
    children = object?

    I'm confused. Mostly about the children thing as I got a huge error.

    Using Firebug output was:
    Code:
    uncaught exception: [Exception... "Could not convert JavaScript argument" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: file:///D:/robgar%20webmania/test%20pages/Grapher/TMPr65eqgcqx9.htm :: addDiv :: line 23" data: no]

  4. #4
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Oh yeah

    Global Moderator...
    Well done. Nice fancy title


  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yeah, depressing, isn't it?

    Anyway... parent is a DOM node, id is a string, style is an object containing key:value pairs of styles, and children is an array or collection of elements.
    Code:
    var myDiv = addElement(
      document.body,
      "myDivID",
      {
        'background-color' : "red",
        'position' : "absolute",
        'left', "0"
      },
      document.getElementsByTagName("span")
    );
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Thanks

    Thanks Twey,

    I think you just bumped my knowledge of JS up a lot!
    I don't usually use objects like that.
    nice one

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Twey, so you can use the for in loop for objects that aren't html elements? Neat... I'm going to play for an hour with this one.
    - Mike

Bookmarks

Posting Permissions

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