how to tell if a variable is a reference to an HTML Element"<select>"?

Z

Zhang Weiwu

Dear all

How does javascript realiablily tell if a variable is a reference to an
HTML Element "<select>", without causing browser to pop up error message?

if (variable.constructor == HTMLElementSelect)
// doesn't work, Firefox complain no such property if variable is undefined

if (typeof variable == "object" && variable.constructor == HTMLElementSelect)
// doesn't work, IE complain object do not have this property "constructor"
// However, IE do show constructor of objects if it's not Element Object

if (variable.tagName == "select")
// doesn't work if variable is undefined.

if (typeof variable == "object" && variable.tagName == "select")
// doesn't work, complain property do not exist if object is an non DOM Node

if (typeof variable == "object" && variable.tagName && variable.tagName == "select")
// must the working solution be this complicated?

Thanks, sorry I didn't read other people's code enough to have seen how
tho handle this neatly. And I didn't google out either.
 
L

lucas.e.smith

Dear all

How does javascript realiablily tell if a variable is a reference to an
HTML Element "<select>", without causing browser to pop up error message?

if (variable.constructor == HTMLElementSelect)
// doesn't work, Firefox complain no such property if variable is undefined

if (typeof variable == "object" && variable.constructor == HTMLElementSelect)
// doesn't work, IE complain object do not have this property "constructor"
// However, IE do show constructor of objects if it's not Element Object

if (variable.tagName == "select")
// doesn't work if variable is undefined.

if (typeof variable == "object" && variable.tagName == "select")
// doesn't work, complain property do not exist if object is an non DOM Node

if (typeof variable == "object" && variable.tagName && variable.tagName == "select")
// must the working solution be this complicated?

Thanks, sorry I didn't read other people's code enough to have seen how
tho handle this neatly. And I didn't google out either.

if (variable && typeof(variable.nodeName) !== 'undefined' &&
variable.nodeName === 'SELECT') {
...
}
 
S

scripts.contact

Dear all

How does javascript realiablily tell if a variable is a reference to an
HTML Element "<select>", without causing browser to pop up error message?

if(variable && (varibale instanceof HTMLSelectElement) )
alert("variable is a select element")
 
Z

Zhang Weiwu

于 Fri, 06 Jul 2007 10:24:45 -0700,scripts.contact写到:
if(variable && (varibale instanceof HTMLSelectElement) )
alert("variable is a select element")

"Error: HTMLSelectElement undefined.", says IE6 politely.
 
Z

Zhang Weiwu

于 Fri, 06 Jul 2007 08:51:10 -0700,lucas.e.smith写到:
if (variable && typeof(variable.nodeName) !== 'undefined' &&
variable.nodeName === 'SELECT') {
...
}

Thank you, this works for both IE and Firefox. Just as I thought, with IE
usually there is no simple way.
 
T

The Natural Philosopher

Zhang said:
于 Fri, 06 Jul 2007 08:51:10 -0700,lucas.e.smith写到:


Thank you, this works for both IE and Firefox. Just as I thought, with IE
usually there is no simple way.

As an aside, what is the meaning of '!==' and '===' ?
 
T

The Natural Philosopher

Randy said:
The Natural Philosopher said the following on 7/8/2007 4:12 PM:



!== means it is NOT an exact match.
=== means it is an exact match.

And "match" includes the constructor and all. The undefined and null ==
another but they are not === to one another. The major difference, to me
(and easiest way for me to keep up with it) is that == does a type
conversion and === does not. The ! simply means NOT.
TVM!
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top