Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > how to tell if a variable is a reference to an HTML Element"<select>"?

Reply
Thread Tools

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

 
 
Zhang Weiwu
Guest
Posts: n/a
 
      07-06-2007
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.
 
Reply With Quote
 
 
 
 
lucas.e.smith@gmail.com
Guest
Posts: n/a
 
      07-06-2007
On Jul 6, 4:33 am, Zhang Weiwu <(E-Mail Removed)> wrote:
> 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') {
...
}

 
Reply With Quote
 
 
 
 
scripts.contact
Guest
Posts: n/a
 
      07-06-2007
On Jul 6, 4:33 am, Zhang Weiwu <(E-Mail Removed)> wrote:
> 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")

 
Reply With Quote
 
Zhang Weiwu
Guest
Posts: n/a
 
      07-08-2007
于 Fri, 06 Jul 2007 10:24:45 -0700,scripts.contact写到:

> On Jul 6, 4:33 am, Zhang Weiwu <(E-Mail Removed)> wrote:
>> 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.
 
Reply With Quote
 
Zhang Weiwu
Guest
Posts: n/a
 
      07-08-2007
于 Fri, 06 Jul 2007 08:51:10 -0700,lucas.e.smith写到:

> On Jul 6, 4:33 am, Zhang Weiwu <(E-Mail Removed)> wrote:
>> 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.
 
Reply With Quote
 
The Natural Philosopher
Guest
Posts: n/a
 
      07-08-2007
Zhang Weiwu wrote:
> 于 Fri, 06 Jul 2007 08:51:10 -0700,lucas.e.smith写到:
>
>> On Jul 6, 4:33 am, Zhang Weiwu <(E-Mail Removed)> wrote:
>>> 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 '===' ?
 
Reply With Quote
 
The Natural Philosopher
Guest
Posts: n/a
 
      07-09-2007
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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Some people tell that at present, most web hosting servers supportall kinds of programming language, some people tell me that many web hostingserver don't support Java, What is the truth? Erwin Moller Java 3 05-07-2008 05:09 PM
CAN any one tell it whats the code tell it yogesh C++ 1 03-14-2007 01:12 PM
How to tell if a reference is project or file reference from within the IDE? Darren ASP .Net 0 10-11-2004 12:51 AM
Script to Tell Tell Time then Limit by it? Mike A Javascript 17 11-19-2003 02:16 PM



Advertisments