We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,011 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

'document.getElementById(...)' is null or not an object.

//JavaScript
function checkCountry(slctFild)
{
	if(slctFild.selectedIndex==1)
	{
		showStateSelectBox();
		hideProvinceSelectBox();
	}
	else
	{
		if(slctFild.selectedIndex==36)
		{
			hideStateSelectBox();
			showProvinceSelectBox();
		}
		else
		{
			document.getElementById("00N200000013FlZ").selectedIndex = 0; // ERROR gets thrown here!!!
			document.getElementById("00N200000013Flo").selectedIndex = 0;
			hideStateSelectBox();
			hideProvinceSelectBox();
		}
	}
}
//HTML Registraton Form

<select id="00N200000013FlU" title="Country" onchange="checkCountry(this)" name="00N200000013FlU">
	<option value="" selected="selected">--None--</option>
	<option value="United States of America">United States of America</option>
	...
</select>
                
<select id="00N200000013FlZ" title="State" name="00N200000013FlZ">
	<option value="" selected="selected">--None--</option>
	<option value="Alabama">Alabama</option>
	...
</select>
				
<select id="00N200000013Flo" title="Province" name="00N200000013Flo">
	<option value="" selected="selected">--None--</option>
	<option value="Alberta">Alberta</option>
	...
</select>

I get an error message every so often with this code.
'document.getElementById(...)' is null or not an object.
Ive added a comment to the line that i receive the error from. I cant seem to Find the error.

Any Help?
Thanx

9
Contributors
9
Replies
2 Years
Discussion Span
1 Year Ago
Last Updated
10
Views
Silo45
Newbie Poster
9 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I had a look at the code and tried it out, it works for me (though I did get rid of those "..." things you have. I was using firefox.

sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 65
Skill Endorsements: 0

See thats the problem.

I have a Mailer set up, and every once it awhile i get an email with the error.

It wasnt a big deal until last week i got 5 in just a coupe of days.

I am unable to to reproduce the error, I guess thats why im have a hard time fixing it.

Silo45
Newbie Poster
9 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

> I get an error message every so often with this code.
> 'document.getElementById(...)' is null or not an object.

Because there exists no element with the given ID. It looks as though the ID for your form elements are dynamically generated and hence the intermittent problem. A better way for setting the default drop down option would be to grab hold of all the SELECT items and set their selectedIndex property to zero (except for the SELECT element under consideration)

var sels = document.getElementsByTagName("SELECT");
for(var i = 0, maxI = sels.length; i < maxI; ++i) {
  var sel = sels[i];
  if(sel == slctFild)
    continue;  // exclude the current select element
  sel.selectedIndex = 0;
}
~s.o.s~
Failure as a human
Administrator
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 55

You just dumped huge compacted code portion to the forum. Sorry, I am not going to sit down and look at it. Also, please do not hijack other's thread. You should create your own.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

Anybody could help me on this javascript which runs well in every browser except IE, I cant figure out why this strange error occurs. "null is null or not an object error"

        function fetchEventSource(src, callback) {
            alert(src);
            alert(callback);
            var prevViewName = view.name,
            prevDate = cloneDate(date),
            reportEvents = function(a) {
                if (prevViewName == view.name && +prevDate == +date  // protects from fast switching
                    && $.inArray(src, eventSources) != -1)               // makes sure source hasn't been removed
                {
                    alert(a.length);
                    for (var i = 1; i < a.length; i++) {
                        alert(a[i]);
                        normalizeEvent(a[i], options);

                        a[i].source = src;
                    }
                    events = events.concat(a);
                    if (callback) {
                        callback(a);
                    }
                }
            },
            reportEventsAndPop = function(a) {
                reportEvents(a);
                popLoading();
            };
            if (typeof src == 'string') {
                var params = {};
                params[options.startParam] = Math.round(eventStart.getTime() / 1000);
                params[options.endParam] = Math.round(eventEnd.getTime() / 1000);
                if (options.cacheParam) {
                    params[options.cacheParam] = (new Date()).getTime(); // TODO: deprecate cacheParam
                }
                pushLoading();
                $.ajax({
                    url: src,
                    dataType: 'json',
                    data: params,
                    cache: options.cacheParam || false, // don't let jquery prevent caching if cacheParam is being used
                    success: reportEventsAndPop
                });
            }
            else if ($.isFunction(src)) {
                pushLoading();
                src(cloneDate(eventStart), cloneDate(eventEnd), reportEventsAndPop);
            }
            else {
                reportEvents(src); // src is an array
            }
        }
binesh.uba
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Learn any JS library.
It will practically save you alot of pain with javascript ;)

Stefano Mtangoo
Senior Poster
3,731 posts since Jun 2007
Reputation Points: 462
Solved Threads: 396
Skill Endorsements: 0

it is simple you have to write a code after the elemet and then you can get the element by id (when the mashin know the element)

punk24
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

just add condition before you fetching value with document.getElementByID or getElementsByName. condition looks like: if(document.getElementById() && document.getElementById()!='undefined')
or
if(document.getElementsByName()[0] && document.getElementsByName()[0]!='undefined')


May help you to avoid the javascript errors while loading and with no value fetching from the controls.

Thanks,
Raghuram Reddy Gottimukkula
Adobe Certified ColdFusion Professional,
Hyderabad, India

raghuramgreddy
Newbie Poster
1 post since Aug 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

3 years old thread!

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0817 seconds using 2.7MB