Checking if span object(s) exist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Uzi G.

    Checking if span object(s) exist

    Hi,

    We have a JSP website intended for viewing with IE 5 (and above)
    On a certain jsp page, there are few span objects
    (<SPAN ID="name">text </SPAN>), which are created dynamically.

    The spans share the same ID, so we are able to reference them as an
    array, in the javascript code :
    for example:

    <SPAN ID="wow">lala</SPAN>
    <SPAN ID="wow")kaka</SPAN>

    in the JS code:

    wow[0].innerHTML = "fafa";
    wow[1].innerHTML = "zaza";

    It may occur, since these spans are created dynamically by the JSP
    page, that on a certain instance of the page, no spans are created.

    My question is - how do we know, in the JS code, that no spans of ID
    'wow' exist?
    We tried several ways:

    if (!wow) alert('no wow'); // If there were no spans, this gives a
    js error

    tried a few other methods which i cant recall of now..
    I know of a workaround - to add a 'dummy' span with the 'wow' ID , but
    I prefer to know if there is a better way to deal with this.

    Thanks in advance,
    Uzi
  • kaeli

    #2
    Re: Checking if span object(s) exist

    In article <2b223b69.03100 10723.72e5442f@ posting.google. com>,
    antishlook@yaho o.com enlightened us with...[color=blue]
    > We tried several ways:
    >
    > if (!wow) alert('no wow'); // If there were no spans, this gives a
    > js error
    >[/color]

    if (!document.getE lementById("wow ") || document.getEle mentById("wow") ==
    null || document.getEle mentById("wow") == "undefined" )
    alert ('no wow');

    --
    -------------------------------------------------
    ~kaeli~
    All I ask for is the chance to prove that money
    cannot make me happy.


    -------------------------------------------------

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Checking if span object(s) exist

      antishlook@yaho o.com (Uzi G.) writes:
      [color=blue]
      > We have a JSP website intended for viewing with IE 5 (and above)[/color]

      I was just about to stop reading here.
      [color=blue]
      > On a certain jsp page, there are few span objects
      > (<SPAN ID="name">text </SPAN>), which are created dynamically.
      >
      > The spans share the same ID, so we are able to reference them as an
      > array, in the javascript code :
      > for example:
      >
      > <SPAN ID="wow">lala</SPAN>
      > <SPAN ID="wow")kaka</SPAN>[/color]

      I assume the ")" is a type. The generated code is invalid HTML. The ID
      attribute must be unique in a page. Anything else is asking for trouble.
      .... but if IE allows it, I guess you can ignore correctness.
      [color=blue]
      > It may occur, since these spans are created dynamically by the JSP
      > page, that on a certain instance of the page, no spans are created.
      >
      > My question is - how do we know, in the JS code, that no spans of ID
      > 'wow' exist?[/color]

      You are also using the IE-specific way of accessing the named
      elements: as a global variables of the same name. That means that
      if there are no spans with that id, there is no variable either.
      [color=blue]
      > We tried several ways:
      >
      > if (!wow) alert('no wow'); // If there were no spans, this gives a
      > js error[/color]

      Yes, because there is no global variable called "wow", and it is an error
      to access an undeclared variable.
      [color=blue]
      > tried a few other methods which i cant recall of now..[/color]

      Interesting. Almost any other check would work:

      if (!document.getE lementById("wow ")) ...
      if (!document.all["wow"]) ...
      if (!window.wow) ...

      (As an interesting side node: If you have two elements with the same id,
      and use document.all[id], the returned "array" contains *two* properties
      with the same property name (id). Highly spurious!)

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Grant Wagner

        #4
        Re: Checking if span object(s) exist

        Lasse Reichstein Nielsen wrote:
        [color=blue]
        > antishlook@yaho o.com (Uzi G.) writes:
        >[color=green]
        > > We have a JSP website intended for viewing with IE 5 (and above)[/color]
        >
        > I was just about to stop reading here.
        >[color=green]
        > > On a certain jsp page, there are few span objects
        > > (<SPAN ID="name">text </SPAN>), which are created dynamically.
        > >
        > > The spans share the same ID, so we are able to reference them as an
        > > array, in the javascript code :
        > > for example:
        > >
        > > <SPAN ID="wow">lala</SPAN>
        > > <SPAN ID="wow")kaka</SPAN>[/color]
        >
        > I assume the ")" is a type. The generated code is invalid HTML. The ID
        > attribute must be unique in a page. Anything else is asking for trouble.
        > ... but if IE allows it, I guess you can ignore correctness.
        >[color=green]
        > > It may occur, since these spans are created dynamically by the JSP
        > > page, that on a certain instance of the page, no spans are created.
        > >
        > > My question is - how do we know, in the JS code, that no spans of ID
        > > 'wow' exist?[/color]
        >
        > You are also using the IE-specific way of accessing the named
        > elements: as a global variables of the same name. That means that
        > if there are no spans with that id, there is no variable either.
        >[color=green]
        > > We tried several ways:
        > >
        > > if (!wow) alert('no wow'); // If there were no spans, this gives a
        > > js error[/color]
        >
        > Yes, because there is no global variable called "wow", and it is an error
        > to access an undeclared variable.[/color]

        Use the "typeof" operator to test for the existance of a variable/object
        property:

        if (typeof wow == 'undefined') {
        alert('no wow');
        }

        But as was pointed out earlier, you shouldn't have multiple elements on a
        page with the same id, you'd be much better off using:

        <span id="wow1">...</span>
        <span id="wow2">...</span>
        <!-- etc -->

        --
        | Grant Wagner <gwagner@agrico reunited.com>

        * Client-side Javascript and Netscape 4 DOM Reference available at:
        *


        * Internet Explorer DOM Reference available at:
        *
        Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


        * Netscape 6/7 DOM Reference available at:
        * http://www.mozilla.org/docs/dom/domref/
        * Tips for upgrading JavaScript for Netscape 7 / Mozilla
        * http://www.mozilla.org/docs/web-deve...upgrade_2.html


        Comment

        • W d'Anjos

          #5
          Re: Checking if span object(s) exist

          You can use:

          if (!document.all. wow) alert('no wow');

          I hope this helps.

          -Wagner

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Checking if span object(s) exist

            Grant Wagner <gwagner@agrico reunited.com> writes:
            [color=blue]
            > Use the "typeof" operator to test for the existance of a variable/object
            > property:
            >
            > if (typeof wow == 'undefined') {
            > alert('no wow');
            > }[/color]

            Interesting. I didn't know that "typeof" allowed undeclared variables,
            but reading the ECMAScript specification, it does work as you say.
            Thanks :)
            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            Working...