Drycodez Posted August 17, 2011 Share Posted August 17, 2011 If i wish to walk through the document node tree, how can this be done? HTML: <body><p>some text</p></body> JS: document.firstChild.firstChild.innerHTML="i have changed the p element text!"; why is the above code not working? Link to comment Share on other sites More sharing options...
boen_robot Posted August 17, 2011 Share Posted August 17, 2011 When opening a document as an HTML one, it implicitly adds required elements like <html> and <title>. You need to make sure that either you have a valid HTML document or you serve it as an XML document to prevent these additions.Also, whitespace is counted as a node, so even if the document was served as XML, "document.firstChild.firstChild" would match the first whitespace of the body element. Right now, it's probably matching the whitespace of the implicitly added HTML element, and inherently doesn't work because having something other than one head and one body is invalid. Link to comment Share on other sites More sharing options...
Drycodez Posted August 17, 2011 Author Share Posted August 17, 2011 When opening a document as an HTML one, it implicitly adds required elements like <html> and <title>. You need to make sure that either you have a valid HTML document or you serve it as an XML document to prevent these additions.Also, whitespace is counted as a node, so even if the document was served as XML, "document.firstChild.firstChild" would match the first whitespace of the body element... thanks! Link to comment Share on other sites More sharing options...
Drycodez Posted August 17, 2011 Author Share Posted August 17, 2011 If want to access element(s) in the body tag, the following works fine: document.body but why is the following not working document.lastChild ??? Link to comment Share on other sites More sharing options...
jeffman Posted August 17, 2011 Share Posted August 17, 2011 document.lastChild will not refer to the last element in the body, if that's what you're thinking. As with people, a child is an immediate descendant. The last child of document should be the <html> element. (I suppose it could be a text node, also.) Link to comment Share on other sites More sharing options...
Drycodez Posted August 17, 2011 Author Share Posted August 17, 2011 document.lastChild will not refer to the last element in the body, if that's what you're thinking. As with people, a child is an immediate descendant. The last child of document should be the <html>... hmmm, kinda confusing! i thought the last child of a document is the <body> tag! What do you mean? Plz make me understand! Link to comment Share on other sites More sharing options...
jeffman Posted August 17, 2011 Share Posted August 17, 2011 Assuming the document is well formed, the <body> element is the last element contained by the <html> element. The document itself contains (1) the <!DOCTYPE> element and (2) the <html> element. Link to comment Share on other sites More sharing options...
Drycodez Posted August 17, 2011 Author Share Posted August 17, 2011 Assuming the document is well formed, the <body> element is the last element contained by the <html> element. The document itself contains (1) the <!DOCTYPE> element and (2) the <html> element.thanks alot!!! Link to comment Share on other sites More sharing options...
Drycodez Posted August 17, 2011 Author Share Posted August 17, 2011 Thanks DD! Link to comment Share on other sites More sharing options...
justsomeguy Posted August 17, 2011 Share Posted August 17, 2011 Why don't you figure out what elements you're trying to reference? Use Firebug and console.log to examine the elements you're working with and then you can figure out why it doesn't do what you expect. You can look at the entire DOM tree through Firebug. Link to comment Share on other sites More sharing options...
Drycodez Posted August 17, 2011 Author Share Posted August 17, 2011 Why don't you figure out what elements you're trying to reference? Use Firebug and console.log to examine the elements you're working with and then you can figure out why it doesn't do what you expect. You can look at the entire DOM tree through Firebug.ok, thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.