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

Discussion in 'Javascript' started by Zhang Weiwu, Jul 6, 2007.

  1. Zhang Weiwu

    Zhang Weiwu Guest

    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.
    Zhang Weiwu, Jul 6, 2007
    #1
    1. Advertising

  2. Zhang Weiwu

    Guest

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

    On Jul 6, 4:33 am, Zhang Weiwu <> 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') {
    ...
    }
    , Jul 6, 2007
    #2
    1. Advertising

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

    On Jul 6, 4:33 am, Zhang Weiwu <> 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")
    scripts.contact, Jul 6, 2007
    #3
  4. Zhang Weiwu

    Zhang Weiwu Guest

    于 Fri, 06 Jul 2007 10:24:45 -0700,scripts.contact写到:

    > On Jul 6, 4:33 am, Zhang Weiwu <> 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.
    Zhang Weiwu, Jul 8, 2007
    #4
  5. Zhang Weiwu

    Zhang Weiwu Guest

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

    > On Jul 6, 4:33 am, Zhang Weiwu <> 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.
    Zhang Weiwu, Jul 8, 2007
    #5
  6. Re: how to tell if a variable is a reference to an HTML Element "<select>"?

    Zhang Weiwu wrote:
    > 于 Fri, 06 Jul 2007 08:51:10 -0700,lucas.e.smith写到:
    >
    >> On Jul 6, 4:33 am, Zhang Weiwu <> 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 '===' ?
    The Natural Philosopher, Jul 8, 2007
    #6
  7. Re: how to tell if a variable is a reference to an HTML Element "<select>"?

    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!
    The Natural Philosopher, Jul 9, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren
    Replies:
    0
    Views:
    480
    Darren
    Oct 11, 2004
  2. Replies:
    1
    Views:
    434
    Willem
    Jun 9, 2006
  3. yogesh
    Replies:
    1
    Views:
    350
    Victor Bazarov
    Mar 14, 2007
  4. cosmo
    Replies:
    0
    Views:
    1,415
    cosmo
    Oct 4, 2008
  5. Mike A
    Replies:
    17
    Views:
    244
    Dr John Stockton
    Nov 19, 2003
Loading...

Share This Page