SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot maileen's Avatar
    Join Date
    Apr 2006
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question insert HTML code amongs tags

    Hi,

    i have an output variable which holds the following code :
    output ="<div id=id_root >Root<div id=id_node1 >Node1</div><div id=id_node2 >Node2</div></div>";

    based on some tests (e.g. on id value), i would like to insert next <div></div> tag.

    e.g.

    if(newnode.parent == id_node1)
    {
    // insert <div .... >some text </div> just after the "Node1" text.
    }

    result should look like this :

    output ="<div id=id_root >Root<div id=id_node1 >Node1<div id=id_node3 >Node3</div></div><div id=id_node2 >Node2</div></div>";

    i was thinking to check InnerText or something like that...but since it is stored into a simple string var, i must search a string into output variable and if found, detect end of the relative <div> tag.

    it's no great to do such thing, so i would like to know if it exists another way ?

    thanks a lot

    M

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,715
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    How about some dom scripting?

    Code JavaScript:
    node1 = document.createElement('div');
    node1.setAttribute('id', 'id_node1');
    node2 = document.createElement('div');
    node2.setAttribute('id', 'id_node2');
     
    if (newnode.parentNode.getAttribute('id') = 'id_node1') {
      // append empty copy
      // node1.appendChild(newnode.cloneNode(false);
      // append full copy
      node1.appendChild(newnode.cloneNode(true);
    }
    root = document.createElement('div');
    root.setAttribute('id', 'id_root');
    root.appendChild(node1);
    root.appendChild(node2);

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would be better to enclose the attribute values in double quotes, and the whole string in single quotes.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,715
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    To expand on that:

    Code JavaScript:
    output ="<div id=id_root >Root<div id=id_node1 >Node1</div><div id=id_node2 >Node2</div></div>";

    is better presented as

    Code JavaScript:
    output ='<div id="id_root">Root<div id="id_node1">Node1</div><div id="id_node2">Node2</div></div>';

    However, having the HTML code stored as a string is bad anyway, whichever way you look at it.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    flawed but useful answer??
    Code:
    node.insertBefore(newNode,document.getElmentById(id).nextSibling());
    Mashups, DOM Scripting and all the things we call Web2.0 blogger
    http://www.heavyweightgeek.org/
    http://twitter.com/heavyweightgeek


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
  •