Results 1 to 9 of 9

Thread: DOM Issue...

  1. #1
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation DOM Issue...

    Hello guys ,

    This time I have started learning a little DOM and have gotten in a prediciment... -

    <html>
    <body onclick="alert(document.body.childNode.nodeValue)">
    <input type="text" id="textb">
    </body>
    </html>

    Though I could use document.getElementById(ID).innerHTML, but by one of the true scripting masters and some other people I have been told its not a good idea to do so...

    Thanks ...

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    childNode isn't anything in javascript because there could be one or thousands. The childNodes array or collection may be used but, it's overkill here.

    innerHTML would be fine if the element has that property and all you wish to do is retrieve it. In the example that you have, it looks like you are going for:

    document.getElementById('textb').value
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You got it all wrong (guess I should have explained a little more )...

    I'm learning DOM and just want to know how I would get to the elemnt using straight forward DOM (no document.getElementById... Or InnerHTML or value (though this stuff is DOM, I already know how to use it which is why))...

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Start of using valid DOCTYPE before using DOM
    ...and then have a good read at this
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I've got it wrong? Some things were just not meant to be done with DOM nodes, and your little example is just about as close as one can get to one of those things.

    If you want to learn about DOM nodes, tech_support's advice is well taken.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hmmmm, OK well I see...

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    John's right, the value property is much more appropriate in this case.

    You can access it as document.body.firstChild.value, but also, as tech_support said, you should be using a valid page before attempting to manipulate the DOM. There are too many possible variables in an invalid page, things are likely to break.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    You can access it as document.body.firstChild.value
    Unless I am mistaken, even using firstChild and a valid page will cause problems in some browsers like FF which will see an empty text node as the body element's firstChild. With a strict DOCTYPE, I don't think the input element could ever be valid and at the same time be the body element's firstChild.

    In my experience, when seeking DOM nodes, it is almost always easier to look for the parent rather than the child.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Unless I am mistaken, even using firstChild and a valid page will cause problems in some browsers like FF which will see an empty text node as the body element's firstChild. With a strict DOCTYPE, I don't think the input element could ever be valid and at the same time be the body element's firstChild.
    Right on both counts... I'd forgotten about the former, and the latter was the reason for the "but" in my post
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •