Checking if span object(s) exist

Discussion in 'Javascript' started by Uzi G., Oct 1, 2003.

  1. Uzi G.

    Uzi G. Guest

    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
    Uzi G., Oct 1, 2003
    #1
    1. Advertising

  2. Uzi G.

    kaeli Guest

    In article <>,
    enlightened us with...
    > We tried several ways:
    >
    > if (!wow) alert('no wow'); // If there were no spans, this gives a
    > js error
    >


    if (!document.getElementById("wow") || document.getElementById("wow") ==
    null || document.getElementById("wow") == "undefined")
    alert ('no wow');

    --
    -------------------------------------------------
    ~kaeli~
    All I ask for is the chance to prove that money
    cannot make me happy.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    -------------------------------------------------
    kaeli, Oct 1, 2003
    #2
    1. Advertising

  3. (Uzi G.) writes:

    > We have a JSP website intended for viewing with IE 5 (and above)


    I was just about to stop reading here.

    > 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>


    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.

    > 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?


    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.

    > We tried several ways:
    >
    > if (!wow) alert('no wow'); // If there were no spans, this gives a
    > js error


    Yes, because there is no global variable called "wow", and it is an error
    to access an undeclared variable.

    > tried a few other methods which i cant recall of now..


    Interesting. Almost any other check would work:

    if (!document.getElementById("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 -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Oct 1, 2003
    #3
  4. Uzi G.

    Grant Wagner Guest

    Lasse Reichstein Nielsen wrote:

    > (Uzi G.) writes:
    >
    > > We have a JSP website intended for viewing with IE 5 (and above)

    >
    > I was just about to stop reading here.
    >
    > > 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>

    >
    > 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.
    >
    > > 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?

    >
    > 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.
    >
    > > We tried several ways:
    > >
    > > if (!wow) alert('no wow'); // If there were no spans, this gives a
    > > js error

    >
    > Yes, because there is no global variable called "wow", and it is an error
    > to access an undeclared variable.


    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 <>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *
    http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

    * 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-developer/upgrade_2.html
    Grant Wagner, Oct 2, 2003
    #4
  5. Uzi G.

    W d'Anjos Guest

    You can use:

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

    I hope this helps.

    -Wagner
    W d'Anjos, Oct 2, 2003
    #5
  6. Grant Wagner <> writes:

    > Use the "typeof" operator to test for the existance of a variable/object
    > property:
    >
    > if (typeof wow == 'undefined') {
    > alert('no wow');
    > }


    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 -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Oct 3, 2003
    #6
    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. Fulio Open

    Can span include span?

    Fulio Open, Jun 26, 2009, in forum: HTML
    Replies:
    5
    Views:
    529
    dorayme
    Jun 26, 2009
  2. Stéphane Klein
    Replies:
    2
    Views:
    1,724
    John Nagle
    Mar 30, 2010
  3. Stefan Behnel
    Replies:
    0
    Views:
    470
    Stefan Behnel
    Mar 29, 2010
  4. Dan Bishop

    DataGrid (body only) contained in <span>...</span> tags

    Dan Bishop, Jun 7, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    254
    Kilic Beg
    Jun 7, 2004
  5. Wang, Jay
    Replies:
    5
    Views:
    448
    Wang, Jay
    May 25, 2004
Loading...

Share This Page