DOM Navigation in IE

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

    DOM Navigation in IE


    I have a problem navigating a form in a table in IE using
    the DOM. I can do what I want in Firefox, but not in IE.
    The rest of my question and information is embedded
    in the following code. Sorry, I don't have a web page to
    publish it in.

    I have tried to make it so it will copy and paste OK.

    <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html><head>
    <title> Clone Puzzle </title>

    <script type="text/javascript">
    function reportKids(thin g,txt) {
    var f = thing.childNode s;
    var preTxt = '';
    var msg = txt;
    for (var i=0; i<f.length; ++i){
    msg += '\nChild ' + i + ' is a ' + f[i];
    var g = f[i].childNodes;
    for (var j=0; j<g.length; ++j){
    preTxt = '\nChild ' + i + ', subChild '
    msg += preTxt +
    j + ' is a ' + g[j] +
    ' of type ' + g[j].nodeType;
    if (g.childNodes){
    msg += '\n.....More kids this node';
    } else {
    msg += '\n.....No more kids this node';
    }
    if (g.nextSibling) {
    msg += '\n.....Some sibs this node';
    } else {
    msg += '\n......No sibs this node';
    } } }
    alert(msg);
    }

    function addElement(theT hing) {
    if( !document.creat eElement || !document.child Nodes ) {
    window.alert('Y our browser is not DOM compliant');
    } else {
    var thisRow = theThing.parent Node.parentNode ;
    var newElement = thisRow.cloneNo de(true);
    reportKids(newE lement,'After clone, before add');

    // The following lines work perfectly in Firefox but not IE
    // newElement.chil dNodes[0].childNodes[1].reset();
    // newElement.chil dNodes[0].childNodes[1].className = 'formBlur';

    thisRow.parentN ode.insertBefor e(newElement,th isRow);
    reportKids(newE lement,'After add to table');
    }}
    </script>
    </head>
    <body style="font-family: sans-serif; color: #555555">
    <table cellpadding="3" ><tr><td width="400">

    <p>Put some text in the field, then click the "Clone" button.
    Function addElement gets the row that the form is in
    (theThing.paren t.parent), clones it
    and adds the clone to the table.</p>

    <p>Firefox and IE clone everything, including
    the content (value) of the text area. I
    wanted to reset the text area value and change
    the style of the form, but I can't navigate
    to either of them in IE using the tr reference
    (newElement) as a starting point.</p>

    <p>addElement shows alert boxes with the tree
    of children of the cloned table row before and
    after adding to the table. The extra "no more
    kids/siblings" tests just show there's nothing more
    to navigate to. The Report Kids button shows the
    same thing for the row in the current page. This
    was to show the tree is exactly the same at each
    of the stages for each particular browser, but
    different across browsers.</p>

    <p>Firefox has everything there, but IE doesn't. How can
    I reset the value of the text field and change the style
    of the cloned form if I can't get to them from the tr
    reference?</p>

    </td></tr>
    <tr><td>
    <form name="aForm">
    <input type="text" name="aText">
    <input
    type="button" name="aButton"
    value="Clone"
    onclick="addEle ment(this.form) ;"
    onfocus="this.b lur();">
    <input type="button" name="bButton"
    value="Report Kids"
    onclick="report Kids(
    this.form.paren tNode.parentNod e,
    'native HTML');"[color=blue]
    >[/color]
    </form>
    </td></tr>
    <tr><td>
    <p>The alert box contains all the children and subchildren of
    the cloned <tr>

    <p>Another interesting thing is that JavaScript has an
    insertBefore method, but not an insertAfter - why not?
    It means I have to put the cloned row before the current
    row, which is not logical for my application. appendChild
    to the table is very inconsistent across browsers, so I
    can't use that (result is navigation issues above x 10!).
    </td></tr></table>

    </body>
    </html


    -
    Rob

  • oeyvind toft

    #2
    Re: DOM Navigation in IE

    Dont know if this is the answer to your problem but:

    replacing:

    newElement.chil dNodes[0].childNodes[1].reset();
    newElement.chil dNodes[0].childNodes[1].className = 'formBlur';

    with:

    newElement.chil dNodes[0].childNodes[0].reset();
    newElement.chil dNodes[0].childNodes[0].className = 'formBlur';

    makes it work fine in my IE6.

    (alert(newEleme nt.childNodes[0].childNodes[0].tagName) returns 'FORM')


    Oeyvind


    --




    "RobG" <RobG.1c7em1@ma il.forum4design ers.com> skrev i melding
    news:RobG.1c7em 1@mail.forum4de signers.com...[color=blue]
    >
    > I have a problem navigating a form in a table in IE using
    > the DOM. I can do what I want in Firefox, but not in IE.
    > The rest of my question and information is embedded
    > in the following code. Sorry, I don't have a web page to
    > publish it in.
    >
    > I have tried to make it so it will copy and paste OK.
    >
    > <!DOCTYPE HTML PUBLIC
    > "-//W3C//DTD HTML 4.0 Transitional//EN">
    > <html><head>
    > <title> Clone Puzzle </title>
    >
    > <script type="text/javascript">
    > function reportKids(thin g,txt) {
    > var f = thing.childNode s;
    > var preTxt = '';
    > var msg = txt;
    > for (var i=0; i<f.length; ++i){
    > msg += '\nChild ' + i + ' is a ' + f[i];
    > var g = f[i].childNodes;
    > for (var j=0; j<g.length; ++j){
    > preTxt = '\nChild ' + i + ', subChild '
    > msg += preTxt +
    > j + ' is a ' + g[j] +
    > ' of type ' + g[j].nodeType;
    > if (g.childNodes){
    > msg += '\n.....More kids this node';
    > } else {
    > msg += '\n.....No more kids this node';
    > }
    > if (g.nextSibling) {
    > msg += '\n.....Some sibs this node';
    > } else {
    > msg += '\n......No sibs this node';
    > } } }
    > alert(msg);
    > }
    >
    > function addElement(theT hing) {
    > if( !document.creat eElement || !document.child Nodes ) {
    > window.alert('Y our browser is not DOM compliant');
    > } else {
    > var thisRow = theThing.parent Node.parentNode ;
    > var newElement = thisRow.cloneNo de(true);
    > reportKids(newE lement,'After clone, before add');
    >
    > // The following lines work perfectly in Firefox but not IE
    > // newElement.chil dNodes[0].childNodes[1].reset();
    > // newElement.chil dNodes[0].childNodes[1].className = 'formBlur';
    >
    > thisRow.parentN ode.insertBefor e(newElement,th isRow);
    > reportKids(newE lement,'After add to table');
    > }}
    > </script>
    > </head>
    > <body style="font-family: sans-serif; color: #555555">
    > <table cellpadding="3" ><tr><td width="400">
    >
    > <p>Put some text in the field, then click the "Clone" button.
    > Function addElement gets the row that the form is in
    > (theThing.paren t.parent), clones it
    > and adds the clone to the table.</p>
    >
    > <p>Firefox and IE clone everything, including
    > the content (value) of the text area. I
    > wanted to reset the text area value and change
    > the style of the form, but I can't navigate
    > to either of them in IE using the tr reference
    > (newElement) as a starting point.</p>
    >
    > <p>addElement shows alert boxes with the tree
    > of children of the cloned table row before and
    > after adding to the table. The extra "no more
    > kids/siblings" tests just show there's nothing more
    > to navigate to. The Report Kids button shows the
    > same thing for the row in the current page. This
    > was to show the tree is exactly the same at each
    > of the stages for each particular browser, but
    > different across browsers.</p>
    >
    > <p>Firefox has everything there, but IE doesn't. How can
    > I reset the value of the text field and change the style
    > of the cloned form if I can't get to them from the tr
    > reference?</p>
    >
    > </td></tr>
    > <tr><td>
    > <form name="aForm">
    > <input type="text" name="aText">
    > <input
    > type="button" name="aButton"
    > value="Clone"
    > onclick="addEle ment(this.form) ;"
    > onfocus="this.b lur();">
    > <input type="button" name="bButton"
    > value="Report Kids"
    > onclick="report Kids(
    > this.form.paren tNode.parentNod e,
    > 'native HTML');"[color=green]
    > >[/color]
    > </form>
    > </td></tr>
    > <tr><td>
    > <p>The alert box contains all the children and subchildren of
    > the cloned <tr>
    >
    > <p>Another interesting thing is that JavaScript has an
    > insertBefore method, but not an insertAfter - why not?
    > It means I have to put the cloned row before the current
    > row, which is not logical for my application. appendChild
    > to the table is very inconsistent across browsers, so I
    > can't use that (result is navigation issues above x 10!).
    > </td></tr></table>
    >
    > </body>
    > </html>
    >
    >
    >
    > --
    > RobG
    >[/color]


    Comment

    • Michael Winter

      #3
      Re: DOM Navigation in IE

      On Tue, 7 Sep 2004 00:43:56 -0500, RobG
      <RobG.1c7em1@ma il.forum4design ers.com> wrote:
      [color=blue]
      > I have a problem navigating a form in a table in IE using
      > the DOM. I can do what I want in Firefox, but not in IE.
      > The rest of my question and information is embedded
      > in the following code. Sorry, I don't have a web page to
      > publish it in.[/color]

      The problem you're seeing is a simple one, but very annoying nevertheless.

      As you should know, elements aren't the only things present in the
      document tree. The text contained within an element, even if it's just
      whitespace (!), is also included. The problem here is that different
      browsers use different rules when building the tree. Whereas some browsers
      (like Firefox) include text nodes between elements, others (like IE) do
      not. When you reference childNodes[0].childNodes[1], the resulting Node is
      a FORM element in Firefox, but nothing in IE. If you referenced
      childNodes[0].childNodes[0], the Node would be a FORM in IE, but a text
      node in Firefox.

      Your only solution is to walk the tree from a known location, checking the
      node type. When you have a text node, skip it. When you have an element
      node, use it!

      [snip]
      [color=blue]
      > <p>Another interesting thing is that JavaScript has an
      > insertBefore method, but not an insertAfter - why not?[/color]

      Because you'd only need one of those. The other is simply a matter of
      using nextSibling or previousSibling . If there is no sibling, you'd get
      null which the method will interpret as an append or prepend[1] operation.
      [color=blue]
      > It means I have to put the cloned row before the current
      > row, which is not logical for my application. appendChild
      > to the table is very inconsistent across browsers, so I
      > can't use that (result is navigation issues above x 10!).[/color]

      Do you really mean appendChild, or are you referring to insertRow? I would
      have thought the latter to be a much better choice. :)

      [snip]

      Good luck,
      Mike


      [1] Should insertAfter exist.

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Michael Winter

        #4
        Re: DOM Navigation in IE

        On Tue, 7 Sep 2004 10:06:05 +0200, oeyvind toft <oeyvtoft@onlin e.no> wrote:
        [color=blue]
        > Dont know if this is the answer to your problem but:
        >
        > replacing:
        >
        > newElement.chil dNodes[0].childNodes[1].reset();
        > newElement.chil dNodes[0].childNodes[1].className = 'formBlur';
        >
        > with:
        >
        > newElement.chil dNodes[0].childNodes[0].reset();
        > newElement.chil dNodes[0].childNodes[0].className = 'formBlur';
        >
        > makes it work fine in my IE6.[/color]

        Yes, but as I said in my post, it'll stop working in Firefox.

        Mike


        Would you please stop top-posting.

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • oeyvind toft

          #5
          Re: DOM Navigation in IE

          Sure .o)

          --




          "Michael Winter" <M.Winter@bluey onder.co.invali d> skrev i melding
          news:opsdx6woiy x13kvk@atlantis ...[color=blue]
          > On Tue, 7 Sep 2004 10:06:05 +0200, oeyvind toft <oeyvtoft@onlin e.no>[/color]
          wrote:[color=blue]
          >[color=green]
          > > Dont know if this is the answer to your problem but:
          > >
          > > replacing:
          > >
          > > newElement.chil dNodes[0].childNodes[1].reset();
          > > newElement.chil dNodes[0].childNodes[1].className = 'formBlur';
          > >
          > > with:
          > >
          > > newElement.chil dNodes[0].childNodes[0].reset();
          > > newElement.chil dNodes[0].childNodes[0].className = 'formBlur';
          > >
          > > makes it work fine in my IE6.[/color]
          >
          > Yes, but as I said in my post, it'll stop working in Firefox.
          >
          > Mike
          >
          >
          > Would you please stop top-posting.
          >
          > --
          > Michael Winter
          > Replace ".invalid" with ".uk" to reply by e-mail.[/color]


          Comment

          Working...