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
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts

    getElementById() syntax error

    Hi everyone, I'm trying to learn JS and I was going OK till I got to this problem:

    I'm trying to get the value of a zipcode field in my form, for validation purposes.

    Based on different tutorials and sites I read, this is the line I am using to try to get the value:

    var zip = document.getElementById("r_zipcode");

    (I know then I access the value through zip.value)

    However, Firefox says there is a syntax error on line 5 (the line above)

    What gives?

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Ok, so I've been playing around with this, and I shifted some lines around. Now it is saying a different line has a syntax error.

    Here is the full code:
    Code:
    function checkZip(zip)
    {
    	if(zip.value != "12345")
    	{		
    		return false;
    	}
    	else
    	{
    		return true;
    	}
    }
    
    function checkForm()
    {
    	zip = document.getElementById("r_zipcode");
    	if(!checkZip(zip))
    	{
    	       return false;
    	}
            else
            {
                  return true;
             }
    }
    Now it says the syntax error is on this line:

    if(zip.value != "12345")

    (I am just testing to see if the zipcode equals 12345 because I haven't gotten into regex with js yet, but I want to see if the code works)

    Thanks
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Ok, I found out what was happening... I wrapped an external JS file in script tags. Stupid, I know, but oh well, I'm learning a lot!



    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #4
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think that your code is correct, but try this code..

    Code:
    function checkZip(zip)
    {
    	zip = document.getElementById(zip).value;
    	if(zip != "12345")
    	{		
    		return false;
    	}
    	else
    	{
    		return true;
    	}
    }
    
    function checkForm()
    {
    	if(!checkZip('r_zipcode'))
    	{
    	       return false;
    	}
            else
            {
                  return true;
             }
    }
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php

  • #5
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Cool, thanks!



    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

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