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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Don't understand why alert(x) will work but not test = x not

    My code is as follows (a snippet):
    Code:
    var newCity;
    var newCenterName;
    
    newCity = center.getElementsByTagName("city")[0].firstChild.nodeValue);
    newCenterName = center.getElementsByTagName("name")[0].firstChild.nodeValue);
    That above code does not work, but if I do:

    alert(center.getElementsByTagName("city")[0].firstChild.nodeValue);
    alert(center.getElementsByTagName("name")[0].firstChild.nodeValue);

    works and it outputs the text. I am trying to save the text so I can use it in my javascript. But i am a real newbie at this XML stuff, can somebody give me a hand?

  • #2
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,453
    Thanks
    1
    Thanked 21 Times in 19 Posts
    Well, your code is correct... and incomplete.

    You see, you set the newCity and newCenterName variables. But you didn't do anything with them. Try, just as a quick test:

    alert(newCity + "\n" + newCenterName);

    Set this after your

    newCenterName = center...
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #3
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am not around my main computer, and I'll definitely try what you said in the morning. Even so, if you are right, it will mean there is a fundamental misunderstanding that I have of DOMs and javascripts.

    Doesn't the line that writes:
    newCity = center.getElementsByTagName("city")[0].firstChild.nodeValue);
    newCenterName = center.getElementsByTagName("name")[0].firstChild.nodeValue);

    do anything? I mean to me it looks like I am setting that variable to the nodeValues of those nodes.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    You have a syntax error there:

    newCity = center.getElementsByTagName("city")[0].firstChild.nodeValue);
    newCenterName = center.getElementsByTagName("name")[0].firstChild.nodeValue);
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm...I was so concerned about my understanding of DOM that I forgot the simple stuff.


  •  

    Posting Permissions

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