Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    problem in getting node value

    can someone please help me to find out wahy I cant access the text value within the "author> tag using the following code? I get that "var y" is null.

    Thanks!

    Code:
    Content-type: text/html\n\n
    <html>
    <body>
    
    <book>
    <author>Moris</author>
    </book>
    
    <script type="text/javascript">
    var x =document.getElementsByTagName("author")[0]
    var y = x.childNodes[0];
    
    document.write(y.value)
    </script>
    
    
    </body>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    <book> and <author> are not HTML tags.

    Code:
    <p>Morris</p>
    <p>Smith</p>
    <p>Robinson</p>
    
    <script type="text/javascript">
    var allParas = document.getElementsByTagName("p");
    var num = allParas.length;
    alert("There are " + num + " <p> elements in this document");
    var y = allParas[2].innerHTML
    alert (y)
    </script>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 12-02-2009 at 11:36 AM.

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    so why is it working here?
    http://www.w3schools.com/Dom/prop_element_tagname.asp

    Quote Originally Posted by Philip M View Post
    Because <book> and <author> are not valid HTML tags.

    Code:
    <p>Morris</p>
    <p>Smith</p>
    <p>Robinson</p>
    
    <script type="text/javascript">
    var allParas = document.getElementsByTagName("p");
    var num = allParas.length;
    alert("There are " + num + " <p> elements in this document");
    var y = allParas[2].innerHTML
    alert (y)
    </script>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by amitye View Post
    in that example the code writes the tagName attribute, not the innerHTML. You must sense the difference between XML DOM and HTML(XHTML) DOM.
    Last edited by Kor; 12-02-2009 at 11:04 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    amitye (12-06-2009)

  • #5
    New Coder
    Join Date
    Dec 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ok. I'm starting to get the difference.
    now lets say that I want to manipulate some XML DOM structure using javascript.

    the origin of this XML is a file which conatin configuration in XML format.
    I dont want to reffer the user directly to open the file, so I want to add it to the html page as a hidden attribute/text.

    what is the best way to do it? and How do I access it after that?

    Quote Originally Posted by Kor View Post
    in that example the code writes the tagName attribute, not the innerHTML. You must sense the difference between XML DOM and HTML(XHTML) DOM.

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New Coder
    Join Date
    Dec 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks, kor.
    I did my study and now I start using ajax . however I have some problem which I was unable to solve.

    The ajax return the xml in the "xmlhttp.responseXML.documentElement"
    where I can parse it and everything is ok.

    The problem start when I want to cop this XML DOM to another variable.
    so I defined a global var "new_dom" and in the "onResponse" (which is being called when the ajax was served) I copy the xml dom from the responseXML to my variable.

    now, when I want to access this variable from other function it is allways null!

    what am i doing wrong??

    Code:
    var new_dom;
    
    function loadXMLDoc(url)
    {
                 xmlhttp=null;
    	if (window.XMLHttpRequest)
    	{// code for IE7+, Firefox, Chrome, Opera, Safari
    		xmlhttp=new XMLHttpRequest();
    	}
    	else
    	{// code for IE5, IE6
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	
    	xmlhttp.onreadystatechange=onResponse;
    	xmlhttp.open("GET",url,true);
    	xmlhttp.send(null);
    	
    	
    }
    
    function onResponse()
    {
    	
    	if(xmlhttp.readyState!=4) return;
    	if(xmlhttp.status!=200)
    	{
    		alert("Problem retrieving XML data");
    		return;
    	}
    	
    	
    	new_dom=xmlhttp.responseXML.documentElement.cloneNode(true)
    		
    }


  •  

    Posting Permissions

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