www.webdeveloper.com
Results 1 to 3 of 3

Thread: HTML DOM - problem with a div

  1. #1
    Join Date
    Feb 2013
    Posts
    9

    HTML DOM - problem with a div

    So I've this little code:

    Code:
    <!DOCTYPE html>
    <html>
    	<head>
    		
    		
    	</head>
    	<body>
    		<div id="first">
    			<p>Hello</p>
    		
    		</div>
    		<script>
    			var x=document.getElementById("first");
    			alert(x.firstChild.innerHTML);
    		</script>
    	</body>
    </html>
    As far as I understand, the <p> inside the <div> should be its child, and in this case the first and last child. In that case, the script should alert "Hello" since it's the innerHTML of the <p> element.
    Obviously I'm missing something and would love to be enlightened.
    Thanks.

  2. #2
    Join Date
    Mar 2011
    Posts
    1,137
    According to w3schools.com, apparently browsers other than Internet Explorer treat white spaces (including line feeds) as text nodes. See http://www.w3schools.com/dom/prop_el...firstchild.asp. So you're probably best off using a function similar to the one they give as an example in order to crawl the child elements to make sure you find the one you really want.
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    Feb 2013
    Posts
    9
    Thanks, that solved my problem. Just one thing, I'm only learning HTML DOM and javascript, and using the doctype HTML, so how come XML rules apply?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles