document.getElementById / appendChild

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    document.getElementById / appendChild

    Hi, I'm having a problem utilizing the DOM. I tried using my own
    code, but it wasn't working, so I tried to use code straight from
    another web site and it verified that the problem may not be mine. I
    am using IE 6.0.28 (sp5) on a Windows 2000 Server.

    I am very new to DOM so I'm not ruling out that I'm doing something
    wrong. However, I am posting the code that I got from another site
    because I did notice that the examples were using Netscape. I'm
    curious to know if there are differences between Netscape and IE for
    DOM? Anyway the site that the code came from is

    and seems like it should work. I'm hoping somebody can help. Thank
    you in advance.... Chris

    <%@ Language=VBScri pt %>
    <html>
    <head>
    <title>Gettin g Sticky</title>
    <style type="text/css">
    * {font-family: sans-serif}
    a {font-size: 6pt}
    .editButton {font-size:6pt}
    </style>

    <script type="text/javascript">
    function getCurrentNumbe r() {
    formElement = document.getEle mentById("noteF orm");
    return formElement.chi ldNodes.item(1) .value;
    }

    function makeNewNote(){
    mainDivElement = document.getEle mentById("mainD iv");
    newNote = document.create Element("div");
    newNote.setAttr ibute("id", "note"+getCurre ntNumber());
    mainDivElement. appendChild(new Note);
    noteForm.total. value= parseInt(noteFo rm.total.value) + 1;
    }
    </script>
    </head>
    <body>
    <div id="mainDiv" style="height:8 5%; width:85%; border:3px solid
    red;
    padding: 10px; z-index:
    -100" >

    <h1>Getting Sticky</h1>

    <form id="noteForm">
    Current number of notes
    <input type="text" name="total" value="0"size=" 3"/>
    <input type="button" value="Add a new note"
    onclick="makeNe wNote()" />
    </form>
    <div id="addhere"> </div>

    </div>
    </body>
    </html>

  • Martin Honnen

    #2
    Re: document.getEle mentById / appendChild



    Chris wrote:
    [color=blue]
    > Hi, I'm having a problem utilizing the DOM. I tried using my own
    > code, but it wasn't working, so I tried to use code straight from
    > another web site and it verified that the problem may not be mine. I
    > am using IE 6.0.28 (sp5) on a Windows 2000 Server.
    >
    > I am very new to DOM so I'm not ruling out that I'm doing something
    > wrong. However, I am posting the code that I got from another site
    > because I did notice that the examples were using Netscape. I'm
    > curious to know if there are differences between Netscape and IE for
    > DOM? Anyway the site that the code came from is
    > http://www-106.ibm.com/developerwork...jsdom/#figure2
    > and seems like it should work. I'm hoping somebody can help. Thank
    > you in advance.... Chris
    >
    > <%@ Language=VBScri pt %>
    > <html>
    > <head>
    > <title>Gettin g Sticky</title>
    > <style type="text/css">
    > * {font-family: sans-serif}
    > a {font-size: 6pt}
    > .editButton {font-size:6pt}
    > </style>
    >
    > <script type="text/javascript">
    > function getCurrentNumbe r() {
    > formElement = document.getEle mentById("noteF orm");
    > return formElement.chi ldNodes.item(1) .value;[/color]

    Using childNodes is a potential problem as IE for instance keeps only
    element nodes in childNodes while it throws out white space text nodes
    between element nodes. Netscape however keeps the white space text nodes
    in the DOM and therefore an expression like
    element.childNo des.item(1)
    is probabably selecting a different node in IE and Netscape.[color=blue]
    > }
    >
    > function makeNewNote(){
    > mainDivElement = document.getEle mentById("mainD iv");
    > newNote = document.create Element("div");
    > newNote.setAttr ibute("id", "note"+getCurre ntNumber());
    > mainDivElement. appendChild(new Note);
    > noteForm.total. value= parseInt(noteFo rm.total.value) + 1;
    > }
    > </script>
    > </head>
    > <body>
    > <div id="mainDiv" style="height:8 5%; width:85%; border:3px solid
    > red;
    > padding: 10px; z-index:
    > -100" >
    >
    > <h1>Getting Sticky</h1>
    >
    > <form id="noteForm">
    > Current number of notes
    > <input type="text" name="total" value="0"size=" 3"/>
    > <input type="button" value="Add a new note"
    > onclick="makeNe wNote()" />
    > </form>
    > <div id="addhere"> </div>
    >
    > </div>
    > </body>
    > </html>
    >[/color]

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Grant Wagner

      #3
      Re: document.getEle mentById / appendChild

      Martin Honnen wrote:
      [color=blue]
      > Chris wrote:
      >[color=green]
      > > Hi, I'm having a problem utilizing the DOM. I tried using my own
      > > code, but it wasn't working, so I tried to use code straight from
      > > another web site and it verified that the problem may not be mine. I
      > > am using IE 6.0.28 (sp5) on a Windows 2000 Server.
      > >
      > > I am very new to DOM so I'm not ruling out that I'm doing something
      > > wrong. However, I am posting the code that I got from another site
      > > because I did notice that the examples were using Netscape. I'm
      > > curious to know if there are differences between Netscape and IE for
      > > DOM? Anyway the site that the code came from is
      > > http://www-106.ibm.com/developerwork...jsdom/#figure2
      > > and seems like it should work. I'm hoping somebody can help. Thank
      > > you in advance.... Chris
      > >
      > > <%@ Language=VBScri pt %>
      > > <html>
      > > <head>
      > > <title>Gettin g Sticky</title>
      > > <style type="text/css">
      > > * {font-family: sans-serif}
      > > a {font-size: 6pt}
      > > .editButton {font-size:6pt}
      > > </style>
      > >
      > > <script type="text/javascript">
      > > function getCurrentNumbe r() {
      > > formElement = document.getEle mentById("noteF orm");
      > > return formElement.chi ldNodes.item(1) .value;[/color]
      >
      > Using childNodes is a potential problem as IE for instance keeps only
      > element nodes in childNodes while it throws out white space text nodes
      > between element nodes. Netscape however keeps the white space text nodes
      > in the DOM and therefore an expression like
      > element.childNo des.item(1)
      > is probabably selecting a different node in IE and Netscape.[/color]

      I can't remember who pointed me to this, but here is a good resource on the
      "problem" described above.

      <url: http://www.mozilla.org/docs/dom/technote/whitespace/ />

      Using "nodomws.js " supplied there, you could re-write the function as:

      function getCurrentNumbe r() {
      formElement = document.getEle mentById("noteF orm");
      var theNode = node_before(nod e_after(formEle ment.childNodes .item(1)));
      if (theNode != null) {
      return theNode.value;
      } else {
      return '';
      }
      }

      and it will work in both IE and Mozilla, regardless of whitespace
      distribution. It messes things up a little because it makes
      childNodes.item (1) the first element in the form (when it should really be
      childNodes.item (0)), but a simple modification to node_after() would fix
      that if you were so inclined.

      --
      | Grant Wagner <gwagner@agrico reunited.com>

      * Client-side Javascript and Netscape 4 DOM Reference available at:
      *


      * Internet Explorer DOM Reference available at:
      *
      http://msdn.microsoft.com/workshop/a...ence_entry.asp

      * Netscape 6/7 DOM Reference available at:
      * http://www.mozilla.org/docs/dom/domref/
      * Tips for upgrading JavaScript for Netscape 7 / Mozilla
      * http://www.mozilla.org/docs/web-deve...upgrade_2.html


      Comment

      Working...