can element id be an array?

Discussion in 'HTML' started by Chamomile, Jan 23, 2004.

  1. Chamomile

    Chamomile Guest

    For simplicity in referencing elements ( in this case div's) for
    manipulating with javascript I have been using an array as the value for
    id's.
    eg:
    <div id='mydiv[0]' >my div 0</div>
    <div id='mydiv[1]'>my div 1</div>

    this works fine even using 2 dimensional arrays for some fairly complicated
    stuff..

    However, one 'section' of a page started giving javascript errors (IE6)
    although it used an identical code style.
    After exhaustive cheking for the usual errors with no obvious culprits I ran
    the page through a validator with the result that I had line after line of
    complaints from the validator that the '[' character could not be used in an
    id value.

    If that's the case then why do some work? Can one really not use an array
    member as an id?
     
    Chamomile, Jan 23, 2004
    #1
    1. Advertising

  2. "Chamomile" <> wrote:

    > For simplicity in referencing elements ( in this case div's) for
    > manipulating with javascript I have been using an array as the
    > value for id's.
    > eg:
    > <div id='mydiv[0]' >my div 0</div>
    > <div id='mydiv[1]'>my div 1</div>


    There are no arrays in HTML. The content of an id attribute is just an
    identifier, with no internal structure from HTML viewpoint. Those
    values are malformed, since the brackets are not permitted in
    identifiers (attributes with ID value).

    > this works fine


    Maybe on sunny days if the browser likes you. It's still incorrect
    markup.

    I suggest that you find some other method to iterate over a set of
    elements in JavaScript. Try comp.lang.javascript after checkings its
    FAQs.

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
    Pages about Web authoring: http://www.cs.tut.fi/~jkorpela/www.html
     
    Jukka K. Korpela, Jan 23, 2004
    #2
    1. Advertising

  3. Chamomile

    Jeff Thies Guest

    > > For simplicity in referencing elements ( in this case div's) for
    > > manipulating with javascript I have been using an array as the
    > > value for id's.
    > > eg:
    > > <div id='mydiv[0]' >my div 0</div>
    > > <div id='mydiv[1]'>my div 1</div>


    Just do this:

    id="id1"

    The javascript would be : var i=1;document.getElementById('id'+i);

    Jeff
     
    Jeff Thies, Jan 24, 2004
    #3
  4. Chamomile

    Richard Guest

    Jeff Thies wrote:

    > >> For simplicity in referencing elements ( in this case div's) for
    > >> manipulating with javascript I have been using an array as the
    > >> value for id's.
    > >> eg:
    > >> <div id='mydiv[0]' >my div 0</div>
    > >> <div id='mydiv[1]'>my div 1</div>


    > Just do this:


    > id="id1"


    > The javascript would be : var i=1;document.getElementById('id'+i);


    > Jeff


    And if the visitor has a non javascript browser, you're screwed.
     
    Richard, Jan 24, 2004
    #4
  5. Chamomile

    Chamomile Guest

    I take the point about using an 'addon' digit rather than an array - and it
    works ok,
    though there are some things that I obviously won't then be able to do.

    The point about non-javascript browsers is true, but there will be an
    alternative.
    (its a kind of dynamic text -tree thing which assumes a javascript enabled
    browser.)

    What puzzles me is why it works using the[n] array format at all!
    It's only the validator that alerted me to it.
    Are the browsers just treating it as a text string anyway? (after all, they
    are different as strings)
    and being kind about the illegal '[' character?
    It works fine in IE, Netscape and Opera.
    I have also used similar arrays before in html id's in long lists generated
    by php
    for web editing databases and that works fine too.
    If it is the case that [] is illegal in an id then I have been innocently
    using it with no problem for some time
    I suppose the first time I tried it, it worked so I assumed it was ok.

    footnote:
    (by the way, the original script was working ok, I had one of those
    cant-see-for-looking typos
    that was causing the error that sent me to the validator in the first
    place)

    "Richard" <anonymous@127.000> wrote in message
    news:...
    > Jeff Thies wrote:
    >
    > > >> For simplicity in referencing elements ( in this case div's) for
    > > >> manipulating with javascript I have been using an array as the
    > > >> value for id's.
    > > >> eg:
    > > >> <div id='mydiv[0]' >my div 0</div>
    > > >> <div id='mydiv[1]'>my div 1</div>

    >
    > > Just do this:

    >
    > > id="id1"

    >
    > > The javascript would be : var i=1;document.getElementById('id'+i);

    >
    > > Jeff

    >
    > And if the visitor has a non javascript browser, you're screwed.
    >
    >
     
    Chamomile, Jan 25, 2004
    #5
  6. Chamomile

    RaulentRoi

    Joined:
    Oct 28, 2013
    Messages:
    1
    Location:
    Paris Fr, Tampa USA
    I am not challenging the fact that brackets are illicit in html. However, I've been using them for over 5 years, and it works fine for all my purposes.

    Here is an example that works in all the usual browsers:

    <html>...
    <form...>
    <input id="myTestId[illicitId]" name="myTestId[illicitId]" type="text" value="nothing" onclick="javascript:TestAssignValue('illicitId')">
    </form>

    <script ...>
    function TestAssignValue(illicitId)
    {
    var tmpvar = 'myTestId['+illicitId+']';
    document.getElementById(tmpvar).value='test';
    }
    </script>

    is equivalent to

    <script ...>
    document.getElementById('myTestId['+illicitId+']').value='test';
    </script>

    and assigns the value: "test" to myTestId[illicitId] (id or/and name)

    You may loop it as much as you want. :)
     
    Last edited: Oct 28, 2013
    RaulentRoi, Oct 28, 2013
    #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. Patient Guy
    Replies:
    8
    Views:
    770
    Roy Schestowitz
    Feb 13, 2005
  2. HANM
    Replies:
    2
    Views:
    750
    Joseph Kesselman
    Jan 29, 2008
  3. Szabolcs Borsanyi
    Replies:
    6
    Views:
    678
    Ben Bacarisse
    May 23, 2008
  4. Al Cholic
    Replies:
    18
    Views:
    290
    Al Cholic
    Jul 28, 2007
  5. \A_Michigan_User\
    Replies:
    4
    Views:
    169
    Thomas 'PointedEars' Lahn
    Nov 16, 2007
Loading...

Share This Page