Insert Into Div

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bewareoffifi@myway.com

    Insert Into Div

    I want the ability to add another form element with the click of a
    button.

    This is what I have now:

    <div id="myID"><INPU T TYPE="text" NAME="MyText"></div>
    <input type="Button" value="Add Another" onclick="addIte m()">

    <script>
    function addItem() {
    myID.innerHTML+ ='<br><input type="Text" name="MyText">' ;
    }
    </script>

    It does the job except if you click on the "Add Another" button, it
    removes everything you have typed into form fields so far.

    Can you use something like document.write to add into a div? Any other
    advice?

    Thanks for the help.

  • web.dev

    #2
    Re: Insert Into Div

    bewareoffifi@my way.com wrote:[color=blue]
    > I want the ability to add another form element with the click of a
    > button.
    >
    > This is what I have now:
    >
    > <div id="myID"><INPU T TYPE="text" NAME="MyText"></div>
    > <input type="Button" value="Add Another" onclick="addIte m()">
    >
    > <script>
    > function addItem() {
    > myID.innerHTML+ ='<br><input type="Text" name="MyText">' ;
    > }
    > </script>
    >
    > It does the job except if you click on the "Add Another" button, it
    > removes everything you have typed into form fields so far.
    >
    > Can you use something like document.write to add into a div? Any other
    > advice?
    >
    > Thanks for the help.[/color]

    There is a similar question here:


    :)

    Comment

    • kaeli

      #3
      Re: Insert Into Div

      In article <1117728977.578 059.232560@z14g 2000cwz.googleg roups.com>,
      bewareoffifi@my way.com enlightened us with...[color=blue]
      >
      > Can you use something like document.write to add into a div? Any other
      > advice?
      >[/color]

      Look at DOM methods createElement, appendChild, insertBefore, and insertAfter
      instead of innerHTML. A bit more to them, but IME, less problematic in the
      long run.
      You'd need to create the new form element and append it to the form.

      --
      --
      ~kaeli~
      Can you be a closet claustrophobic?



      Comment

      • RobG

        #4
        Re: Insert Into Div

        kaeli wrote:[color=blue]
        > In article <1117728977.578 059.232560@z14g 2000cwz.googleg roups.com>,
        > bewareoffifi@my way.com enlightened us with...
        >[color=green]
        >>Can you use something like document.write to add into a div? Any other
        >>advice?
        >>[/color]
        >
        >
        > Look at DOM methods createElement, appendChild, insertBefore, and insertAfter[/color]

        There is no 'insertAfter' method in DOM Level 2, though the
        functionality can be implemented using insertBefore:

        node2 = document.create Element(tag);
        node1.parentNod e.insertBefore( node2, node1.nextSibli ng)

        Where node1 is a reference to some node in the document. node2 will be
        inserted after node1 even if it doesn't have a nextSibling.

        For the OP, the W3C DOM 2 Core is here:

        <URL:http://www.w3.org/TR/DOM-Level-2-Core/core.html>

        Useful assistance with examples is provided at the Mozilla site:

        <URL:http://www.mozilla.org/docs/dom/domref/>

        Remember to feature test before using the DOM's various methods as
        support varies in different UAs.



        --
        Rob

        Comment

        • kaeli

          #5
          Re: Insert Into Div

          In article <MJMne.1863$Zn. 89220@news.optu s.net.au>, rgqld@iinet.net .auau
          enlightened us with...[color=blue]
          > kaeli wrote:[color=green]
          > > In article <1117728977.578 059.232560@z14g 2000cwz.googleg roups.com>,
          > > bewareoffifi@my way.com enlightened us with...
          > >[color=darkred]
          > >>Can you use something like document.write to add into a div? Any other
          > >>advice?
          > >>[/color]
          > >
          > >
          > > Look at DOM methods createElement, appendChild, insertBefore, and insertAfter[/color]
          >
          > There is no 'insertAfter' method in DOM Level 2,[/color]

          *smacks self on head*
          Thanks.
          I should really look stuff up before I post.
          Confusing languages again. Sorry.

          (note: XmlNode.InsertA fter is a .NET method)

          --
          --
          ~kaeli~
          Reading while sunbathing makes you well red.



          Comment

          Working...