K_Drive Posted June 27, 2007 Share Posted June 27, 2007 I am just getting my feet wet with the DOM and changing values with it.I am trying to get a node's value. I am using the OReilly book "Web Design In A Nutshell" examples.Here is my simplified web page where I tried three ways of getting the node value of the first <h5> element.I will also add that, when I get the innerHTML value of the <div id="section01">, I do get all the elements - except that the tags are all uppercase. I tried changing my code to all uppercase, but this did not help. (Could this be an IE problem?)I am using IE 6.When you view the page and click on the [ Button ], you get three alert boxes. Each should display the node value, but the value is showing as being null.Am I missing something simple here?I would appreciate help with this.Sincerely,Ken <html><head><script language="Javascript">function displaySubtitle(theDiv){var text = document.getElementById(theDiv).getElementsByTagName( 'h4' )[0].nodeValuealert("1. Value = "+text)var divId = document.getElementById(theDiv)var elementH4 = divId.getElementsByTagName('h4')var firstH4 = elementH4[0]var firstH4Value = firstH4.nodeValuealert("2. Value = "+firstH4Value)var text03 = document.getElementById(theDiv).firstChild.nodeValuealert("3. Value = "+text)}</script></head><body><div id="section01"> <h4>Subtitle 0001</h4> <h4 onClick="displaySubtitle('section01')">[ Button ]</h4> <p>This is the first paragraph of text in the DOM Document.</p> <p>This is the second paragraph of text in the DOM Document.</p></div></body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted June 27, 2007 Share Posted June 27, 2007 If you're testing this in IE, nodevalue doesn't work correctly there.http://www.google.com/search?client=opera&...-8&oe=utf-8 Link to comment Share on other sites More sharing options...
K_Drive Posted June 27, 2007 Author Share Posted June 27, 2007 Thanks for the answer.I know that I can set the value, but I guess I cannot get it.I was doing some research at MSDN.com and found that .nodeValue does work, but it is different from what I am trying to do. The MSDN web page sets the value. I am trying to get it.Below is the link to the MSDN page.Note: This page has the following quote:"If the object is an element, the nodeValue returns null. . . ."Here is the link:http://msdn2.microsoft.com/en-us/library/ms534192.aspxHere is the script from that page that I modified so that it would be a full, working web page:I will do more research on this.Thanks. I do appreciate your answer.Ken (K_Drive) <html><head><script>function fnChangeValue(oList, iItem){ var i // only perform the operation on lists if (oList.nodeName != "UL" && oList.nodeName != "OL") return false; // only perform the operation if the specified index is // within the acceptable range of available list items if (iItem > oList.childNodes.length -1) return false; // get a reference to the specified list item var oLI = oList.childNodes(i); if (!oLI) return false; // get a reference to the text node contained by the list item var oText = oLI.childNodes(0); // ensure that the node is a text node if (oText.nodeType != 3) return false; var exchangeVal = document.getElementById("oList") var textVal = exchangeVal.getAttribute("text01") var sValue if (textVal == "New Node Value") { exchangeVal.setAttribute("text01", "Old Node Value") sValue = "New Node Value" } else { exchangeVal.setAttribute("text01", "New Node Value") sValue = "Old Node Value" } oText.nodeValue = sValue; return true;}</SCRIPT></head><body><UL ID="oList" text01="Old Node Value" onclick="fnChangeValue(this, 0)"><LI>New Node Value</LI></UL></body></html> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.