Jump to content

How to detect if a node exists?


boen_robot

Recommended Posts

The W3Schools' DOM reference seems to be lacking most of what each method returns, so I was wondering, if I know an element with a certain ID attribute MAY be available, how do I detect if it's present already (and create it if it's not)?What I currently have is:

if (document.getElementById('status') == null) {	var statusDiv = document.createElement('div');	statusDiv.id = 'status';	//Some more code}

Which seems to work, but I'm not sure I see why... I mean, I can't see the big picture in it. Are all methods that lead to non-existing nodes suppose to return "null"?What other (better? more fool proof maybe?) ways are there do ensure that a method (getElementById in this case) returned a result i.e. found node(s) or didn't?

Link to comment
Share on other sites

I usually do something like this:

var statusElement = document.getElementById("status");if(statusElement){	// element with id of 'status' exists.}

However, I think you can do this too:

var statusElement = document.getElementById("status");if(typeof(statusElement) == "undefined"){}

Link to comment
Share on other sites

Are all methods that lead to non-existing nodes suppose to return "null"?
All non-existent properties evaluate to null. And, the getElementById() method is programmed to return null
getElementById introduced in DOM Level 2Returns the Element whose ID is given by elementId. If no such element exists, returns null. Behavior is not defined if more than one element has this ID. Note: The DOM implementation must have information that says which attributes are of type ID. Attributes with the name "ID" are not of type ID unless so defined. Implementations that do not know whether attributes are of type ID or not are expected to return null.
Link to comment
Share on other sites

I usually do something like this:
var statusElement = document.getElementById("status");if(statusElement){	// element with id of 'status' exists.}

However, I think you can do this too:

var statusElement = document.getElementById("status");if(typeof(statusElement) == "undefined"){}

I don't think the first way is a good idea... but I am interested in knowing this typeof() thing better. Where can I find more information about it? I haven't been able to find such so far... anyhow... at least to put it simply - it returns the type of the argument as a string, right? That's basically what I need to know.
All non-existent properties evaluate to null. And, the getElementById() method is programmed to return null
Good to know, thanks. I thought some may evaluate to false, others to "undefined" and the so forth... I was wondering if there's any place where such possible return values are descibed, as W3Schools is lacking all of them.
Link to comment
Share on other sites

I don't think the first way is a good idea...
I think, if you look at this http://www.w3schools.com/js/js_obj_boolean.asp, you'll see that it isn't all that bad of an idea because of the way javascript interprets booleans:0, null, "", false, and NaN all equate to FALSE, everything else, pretty much, equates to TRUE.So, the following are functionally equivalent:
if(statusElement == null){}if(statusElement){}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...