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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Stillwater Ok
    Posts
    226
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Inserting a node between two existing nodes

    If I have:
    Code:
    <form>
      <label>form control</label><input id="text0" />
      <label>form control</label><input id="text1" />
      <label>form control</label><input id="text2" />
    </form>
    Is there a way to insert a node directly after text0 or text1?
    "Never offend people with style when you can offend them with substance."
    --Sam Brown

  • #2
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Since the text fields are form elements, I think the most direct way to do it would just be to specify a span after the text box and append your string to it.

    Something like:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function addNode(pstr,i)
    {
    	var str=document.createTextNode(pstr);
    	var fid="span"+i;
    	document.getElementById(fid).appendChild(str);
    }
    
    </script>
    </head>
    <body onload="addNode('I like hot dogs!',0)">
    <form>
      <label>form control</label><input id="text0" /><span id="span0"></span>
      <label>form control</label><input id="text1" /><span id="span1"></span>
      <label>form control</label><input id="text2" /><span id="span2"></span>
    </form>
    </body>
    </html>
    Basscyst
    Helping to build a bigger box. - Adam Matthews

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, I think I have the solution for you here: Node.prototype.insertBefore(newChild,refChild)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Stillwater Ok
    Posts
    226
    Thanks
    0
    Thanked 0 Times in 0 Posts
    liorean, you are a gentleman and a scholar.

    It works great.
    "Never offend people with style when you can offend them with substance."
    --Sam Brown

  • #5
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Ahh, I see Liorean. I'm just now trying to break my innerHTML habit so all these nodes and children are still kinda new to me. Sometimes answering a question you might not be totally sure about is the best way to learn something new, actually quite often.

    Thanks,
    Basscyst
    Last edited by Basscyst; 01-20-2005 at 12:55 AM.
    Helping to build a bigger box. - Adam Matthews


  •  

    Posting Permissions

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