Newbie: problem with getElementById

J

Jeff

Hey

I'm having problems with the code below, that javascript method don't
displays a message box.. instead the error message "Error on page" is shown
in the statusbar of IE7...

<input type='hidden' id='property' name='property' value='6' />

function NavigateLink() {
var field = document.getElementById('property');
alert(field.value);
}

what am I doing wrong here?

Jeff
 
L

Laurent Bugnion [MVP]

Hi,
Hey

I'm having problems with the code below, that javascript method don't
displays a message box.. instead the error message "Error on page" is shown
in the statusbar of IE7...

<input type='hidden' id='property' name='property' value='6' />

function NavigateLink() {
var field = document.getElementById('property');
alert(field.value);
}

what am I doing wrong here?

Jeff

Most probably there is no field IDed "property" on your page, so
document.getElementById returns null, and then you get a Null reference
exception when you try to access the value property.

var field = document.getElementById('property');
if ( field
&& field.value != null )
{
alert( field.value );
}
else
{
alert( "nope" );
}

HTH,
Laurent
 
J

Jeff

Laurent Bugnion said:
Hi,
Hey

I'm having problems with the code below, that javascript method don't
displays a message box.. instead the error message "Error on page" is
shown in the statusbar of IE7...

<input type='hidden' id='property' name='property' value='6' />

function NavigateLink() {
var field = document.getElementById('property');
alert(field.value);
}

what am I doing wrong here?

Jeff

Most probably there is no field IDed "property" on your page, so
document.getElementById returns null, and then you get a Null reference
exception when you try to access the value property.

var field = document.getElementById('property');
if ( field
&& field.value != null )
{
alert( field.value );
}
else
{
alert( "nope" );
}

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch


Thanks for that tip but it didn't help... however I've made some
discoveries... I think that error isn't related to the code I posted above..
I think the error is in the link to the javascript file. Below you see the
tag that insert the .js file into the web page. In Opera this line is market
red. More specific, I think the error is around src="script/gui.js", gui.js
is located in a subfolder named "script" (located directly under the main
folder).. I also tryed src="/script/gui.js" but get the same error

<script type="text/javascript" src="script/gui.js" ></script>

Any suggestions?

Jeff
 
R

Randy Webb

Jeff said the following on 1/25/2007 5:33 AM:
Thanks for that tip but it didn't help... however I've made some
discoveries... I think that error isn't related to the code I posted above..
I think the error is in the link to the javascript file. Below you see the
tag that insert the .js file into the web page. In Opera this line is market
red. More specific, I think the error is around src="script/gui.js", gui.js
is located in a subfolder named "script" (located directly under the main
folder).. I also tryed src="/script/gui.js" but get the same error

<script type="text/javascript" src="script/gui.js" ></script>

Any suggestions?

Put the contents of gui.js directly into the HTML file.
Debug all script errors.
Move the contents of gui.js back to the external file.
Debug all script errors.
Don't use XHTML on the web.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top