Detecting NodeList object vs Array

C

carlback

I have a function were the input parameter can either be an
string,array,dom node or NodeList (getElementsByTagName()) and I have
somthing like this function which works great for what I want in every
case except if the pNd is the NodeList at which point I end up with the
nodeList in the first element of the array instead of acting like the
array would.

function doSomething(pNd){
if(pNd.constructor != Array){pNd = new Array(pNd)}
return pNd
}

So my question is how is there any easy way to figure out if pNd is a
NodeList ? When I use pNd.constructor on NodeList all I get is [object]


Thanks in advance.

Carl
 
M

Martin Honnen

carlback said:
So my question is how is there any easy way to figure out if pNd is a
NodeList ?

You can detect the features of a NodeList instance, namely the length
property and the item method e.g.
if (typeof nodeList.length != 'undefined' && typeof nodeList.item !=
'undefined')
but such checks are supposed to check for those features to use them
safely if supported.
You seem to want to do something else with a node list, it is not clear
to me what.
 
R

RobG

Martin said:
You can detect the features of a NodeList instance, namely the length
property and the item method e.g.

In some browsers (Opera, Firefox):

if (nodeList instanceof NodeList) { ... }

does the trick - but that's perhaps not broad enough support for some.
:)
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top