By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,159 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Jul 6 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Jul 6, 4:33 am, Zhang Weiwu <zhangwe...@realss.comwrote:
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') {
...
}

Jul 6 '07 #2

P: n/a
On Jul 6, 4:33 am, Zhang Weiwu <zhangwe...@realss.comwrote:
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")

Jul 6 '07 #3

P: n/a
于 Fri, 06 Jul 2007 10:24:45 -0700,scripts.contact写到:
On Jul 6, 4:33 am, Zhang Weiwu <zhangwe...@realss.comwrote:
>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")
"Error: HTMLSelectElement undefined.", says IE6 politely.
Jul 8 '07 #4

P: n/a
于 Fri, 06 Jul 2007 08:51:10 -0700,lucas.e.smith写到:
On Jul 6, 4:33 am, Zhang Weiwu <zhangwe...@realss.comwrote:
>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') {
...
}
Thank you, this works for both IE and Firefox. Just as I thought, with IE
usually there is no simple way.
Jul 8 '07 #5

P: n/a
Zhang Weiwu wrote:
于 Fri, 06 Jul 2007 08:51:10 -0700,lucas.e.smith写到:
>On Jul 6, 4:33 am, Zhang Weiwu <zhangwe...@realss.comwrote:
>>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') {
...
}

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 '===' ?
Jul 8 '07 #6

P: n/a
The Natural Philosopher said the following on 7/8/2007 4:12 PM:

<snip>
As an aside, what is the meaning of '!==' and '===' ?
!== 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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 8 '07 #7

P: n/a
Randy Webb wrote:
The Natural Philosopher said the following on 7/8/2007 4:12 PM:

<snip>
>As an aside, what is the meaning of '!==' and '===' ?

!== 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!
Jul 9 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.