Check if object exists?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris  Ashley

    Check if object exists?

    How do I check if an object exists in Javascript?

    EG:

    if (document.getEl ementById("prod uct").value == null)
    {
    prodValue = "";
    }
    else
    {
    prodValue = document.getEle mentById("produ ct").value;
    }

    This gives me an 'object required' error...

  • [on]

    #2
    Re: Check if object exists?


    Chris Ashley wrote:[color=blue]
    > How do I check if an object exists in Javascript?
    >
    > EG:
    >
    > if (document.getEl ementById("prod uct").value == null)
    > {
    > prodValue = "";
    > }
    > else
    > {
    > prodValue = document.getEle mentById("produ ct").value;
    > }
    >
    > This gives me an 'object required' error...[/color]

    So the element with the id "product" might not exist ?

    var productElement = document.getEle mentById("produ ct");

    productElement should now either be the element or null, if it's null
    you can't check "value" for it.

    so:

    <code>
    var productElement = document.getEle mentById("produ ct");
    if (productElement != null)
    {
    // Code here when the Element Exists.
    }
    </code>

    Comment

    • Randy Webb

      #3
      Re: Check if object exists?

      Chris Ashley said the following on 4/13/2006 5:26 AM:[color=blue]
      > How do I check if an object exists in Javascript?[/color]

      With an if test.
      [color=blue]
      > EG:
      >
      > if (document.getEl ementById("prod uct").value == null)[/color]

      What element has an id of product and does that element have a .value
      property?
      [color=blue]
      > {
      > prodValue = "";
      > }
      > else
      > {
      > prodValue = document.getEle mentById("produ ct").value;
      > }
      >
      > This gives me an 'object required' error...
      >[/color]

      Then you probably do not have an ID of "product", or, that element does
      not have a .value property.

      --
      Randy
      comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
      Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

      Comment

      • MB

        #4
        Re: Check if object exists?

        "Chris Ashley" <chris.ashley2@ gmail.com> skrev i meddelandet
        news:1144920410 .073426.93870@u 72g2000cwu.goog legroups.com...[color=blue]
        > How do I check if an object exists in Javascript?
        >
        > EG:
        >
        > if (document.getEl ementById("prod uct").value == null)
        > {
        > prodValue = "";
        > }
        > else
        > {
        > prodValue = document.getEle mentById("produ ct").value;
        > }
        >
        > This gives me an 'object required' error...
        >[/color]

        if (document.getEl ementById("prod uct") == undefined)
        {
        prodValue = "";
        }
        else
        {
        prodValue = document.getEle mentById("produ ct").value;
        }


        Comment

        • Tony

          #5
          Re: Check if object exists?

          [on] wrote:[color=blue]
          >
          > <code>
          > var productElement = document.getEle mentById("produ ct");
          > if (productElement != null)
          > {
          > // Code here when the Element Exists.
          > }
          > </code>[/color]

          why not

          if (document.getEl ementById('prod uct')) {
          // code here when the element exists
          }

          Comment

          • Dr John Stockton

            #6
            Re: Check if object exists?

            JRS: In article <123t0hatj3vcj8 4@corp.supernew s.com>, dated Thu, 13 Apr
            2006 09:56:00 remote, seen in news:comp.lang. javascript, Tony <tony23@ds
            lextreme.WHATIS THIS.com> posted :[color=blue]
            >
            >why not
            >
            >if (document.getEl ementById('prod uct')) {
            > // code here when the element exists
            >}
            >[/color]


            or, untested but for efficiency,

            if ( T = document.getEle mentById('produ ct') ) {
            // code herein when the element exists, using T
            }

            or

            if ( !(T = document.getEle mentById('produ ct') ) ) { AbandonShip() }
            // code hereon when the element exists, using T

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            • Randy Webb

              #7
              Re: Check if object exists?

              Dr John Stockton said the following on 4/14/2006 1:30 PM:[color=blue]
              > JRS: In article <123t0hatj3vcj8 4@corp.supernew s.com>, dated Thu, 13 Apr
              > 2006 09:56:00 remote, seen in news:comp.lang. javascript, Tony <tony23@ds
              > lextreme.WHATIS THIS.com> posted :[color=green]
              >> why not
              >>
              >> if (document.getEl ementById('prod uct')) {
              >> // code here when the element exists
              >> }
              >>[/color]
              >
              >
              > or, untested but for efficiency,
              >
              > if ( T = document.getEle mentById('produ ct') ) {
              > // code herein when the element exists, using T
              > }
              >
              > or
              >
              > if ( !(T = document.getEle mentById('produ ct') ) ) { AbandonShip() }
              > // code hereon when the element exists, using T
              >[/color]

              The one drawback/possible failure would be in IE where you have an
              element with a name or ID of T, then it would clash and crash. Other
              than that, its ok code.

              --
              Randy
              comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
              Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

              Comment

              • [on]

                #8
                Re: Check if object exists?


                Tony wrote:[color=blue]
                > [on] wrote:[color=green]
                > >
                > > <code>
                > > var productElement = document.getEle mentById("produ ct");
                > > if (productElement != null)
                > > {
                > > // Code here when the Element Exists.
                > > }
                > > </code>[/color]
                >
                > why not
                >
                > if (document.getEl ementById('prod uct')) {
                > // code here when the element exists
                > }[/color]

                Mostly to show the user what's returning "null", and he wanted to grab
                a value of the element later on. Though I could do what Dr Sockton
                said, but I just don't "script/program" like that.

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: Check if object exists?

                  [on] wrote:
                  [color=blue]
                  > Tony wrote:[color=green]
                  >> [on] wrote:[color=darkred]
                  >> > <code>
                  >> > var productElement = document.getEle mentById("produ ct");
                  >> > if (productElement != null)
                  >> > {
                  >> > // Code here when the Element Exists.
                  >> > }
                  >> > </code>[/color]
                  >>
                  >> why not
                  >>
                  >> if (document.getEl ementById('prod uct')) {
                  >> // code here when the element exists
                  >> }[/color]
                  >
                  > Mostly to show the user what's returning "null", and he wanted to grab
                  > a value of the element later on. Though I could do what Dr Sockton
                  > said, but I just don't "script/program" like that.[/color]

                  The reason is a different one. If you do the latter test, you will
                  have to retrieve the reference for the element object, i.e. call
                  document.getEle mentById() twice for the same argument. That is
                  unnecessarily inefficient.

                  However, comparing against `null' is not necessary, and, more important, not
                  complete. The method called is defined in W3C DOM Level 2+ to return
                  `null' if there is no such element. However, it is not defined what to
                  return if there is more than one element that applies (the return value is
                  defined to be undefined; this must be strictly distinguished from the
                  `undefined' value of ECMAScript implementations ). Still it is reasonable
                  to assume that it will not be a true-value then. So the test should be a
                  type-converting one instead:

                  if (productElement )
                  {
                  ... productElement ...
                  }


                  PointedEars

                  Comment

                  • Tony

                    #10
                    Re: Check if object exists?

                    Thomas 'PointedEars' Lahn wrote:[color=blue]
                    > [on] wrote:
                    >[color=green]
                    >>Tony wrote:
                    >>[color=darkred]
                    >>>[on] wrote:
                    >>>
                    >>>><code>
                    >>>>var productElement = document.getEle mentById("produ ct");
                    >>>>if (productElement != null)
                    >>>>{
                    >>>> // Code here when the Element Exists.
                    >>>>}
                    >>>></code>
                    >>>
                    >>>why not
                    >>>
                    >>>if (document.getEl ementById('prod uct')) {
                    >>> // code here when the element exists
                    >>>}[/color]
                    >>
                    >>Mostly to show the user what's returning "null", and he wanted to grab
                    >>a value of the element later on. Though I could do what Dr Sockton
                    >>said, but I just don't "script/program" like that.[/color]
                    >
                    > The reason is a different one. If you do the latter test, you will
                    > have to retrieve the reference for the element object, i.e. call
                    > document.getEle mentById() twice for the same argument. That is
                    > unnecessarily inefficient.[/color]

                    I think it ultimately depends on what you're doing in the // code here
                    when the element exists - after all, it IS possible that you're not
                    doing something to that element :)

                    But - point taken. And that's something I may want to take a look at in
                    my own code.
                    [color=blue]
                    > However, comparing against `null' is not necessary, and, more important, not
                    > complete. The method called is defined in W3C DOM Level 2+ to return
                    > `null' if there is no such element. However, it is not defined what to
                    > return if there is more than one element that applies (the return value is
                    > defined to be undefined; this must be strictly distinguished from the
                    > `undefined' value of ECMAScript implementations ). Still it is reasonable
                    > to assume that it will not be a true-value then. So the test should be a
                    > type-converting one instead:
                    >
                    > if (productElement )
                    > {
                    > ... productElement ...
                    > }[/color]

                    Would that be:
                    var productElement = document.getEle mentById('produ ct');
                    if (productElement ) ...

                    (just for clarity)

                    Comment

                    Working...