PDA

View Full Version : Please help - javascript null object error



rg84
12-16-2009, 11:43 AM
Hi

I am using a custom form elements script in the link below:

www_bristolstreet_co_uk/page/find_a_dealer.htm

and an error for null object is appearing from this line:

active = option[0].childNodes[0].nodeValue;

Can anyone please help? I am a novice at Javascript but got this script working perfectly on my local machine - once I loaded it the live site it didn't work... any advice would be greatly appreciated.

mrhoo
12-16-2009, 03:48 PM
Make sure you have something called option[0]-
maybe it is defined something like option=(selectelement).options;

Your development setup might use the dom differently than what you use online.

Some browsers do not recognize childNodes for option elements-
when they do, an option child nodes nodelist consists of a single text node.
If there is no text in the option, some browsers do not see the 'empty' node.

You could test for it, and use option.text if not found:



// if(!option[0])throw 'No option[0]!';

active = option[0].hasChildNodes()?
option[0].childNodes[0].nodeValue:
option[0].text || '';

you can read or write to option[0].text in any browser.

itsallkizza
12-16-2009, 04:39 PM
If you can post the JS code in question, we can help you create equivalent code that works in all browsers (whether local or remote). DOM parsing used to be such a pain, but as newer browsers take over the marketplace and JS frameworks such as jQuery and Prototype become more commonplace, basic DOM manipulation is becoming easier.