...

View Full Version : getElementById() syntax error



whizard
06-11-2007, 02:17 PM
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

whizard
06-11-2007, 02:31 PM
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:


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

whizard
06-11-2007, 02:58 PM
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!

:D

Dan

smalldog
06-11-2007, 02:59 PM
I think that your code is correct, but try this 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;
}
}

whizard
06-11-2007, 03:18 PM
Cool, thanks!

:)

Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum