Can I create an array of tags by assigning same name?

Discussion in 'Javascript' started by Jenny, Oct 4, 2004.

  1. Jenny

    Jenny Guest

    Hi,

    Can I create an array of tags by assigning same name to these tags?
    For example, I have two <p> tags with the same name t1. But

    document.all.b.value=document.all.t.length

    does not work. It works if the tags are <input type=radio...>. This
    line is OK:

    document.all.btn.value=document.all.b.length


    What are the tags that can be use as an array of tags by assigning
    same name to these tags?


    <HTML><script language="JavaScript">
    function checkButtonSets(){
    //document.all.btn.value=document.all.t.length
    document.all.btn.value=document.all.b.length
    }</script><HEAD>

    <BODY>
    <p name="t" id=q></p>
    <p name="t" id=q></p>
    <input type="Radio" name="b" value="a2">a2
    <input type="Radio" name="b" value="a3">a3<br>
    <input name="btn" type="Button" value="Check Button Sets"
    onClick="checkButtonSets()">
    </BODY></HTML>
    Jenny, Oct 4, 2004
    #1
    1. Advertising

  2. (Jenny) writes:

    > Can I create an array of tags by assigning same name to these tags?


    If I understand your question correctly, then no, not in standard
    compliant browsers.

    > For example, I have two <p> tags with the same name t1.


    <p> tags does not have a "name" attribute in HTML, and if you
    want to use the "id" attribute, they can't have the same value
    either, since id's must be unique.

    Make sure you write correct, validating HTML, or you won't
    be able to predict how Javascript will work on it, especially
    across browsers.

    > But
    >
    > document.all.b.value=document.all.t.length


    "document.all" is a proprietary Microsoft invention. It will not
    work in many other browsers.

    I guess that "b" is the name of a form element, and "t" is the "name"
    of the two <p> elements (in HTML, the (opening and closing) tags
    delimiter the HTML *element*, which is what we are interested in).

    There is no stadard compliant way to get a set of elements by their
    attributes (except document.getElementsByName, which doesn't apply
    to paragraph elements, since they don't have name attributes).

    If they are the only paragraph elements, you can get them as:
    var paragraphs = document.getElementsByTagName("p");
    Otherwise, you will probably have to iterate through the paragraphs,
    or all elements (document.getElementsByTagName("*")) to find the ones
    you want.

    > does not work. It works if the tags are <input type=radio...>. This


    Input elements have "name" attributes.

    > line is OK:
    >
    > document.all.btn.value=document.all.b.length


    I would recommend:
    ---
    document.forms['formName'].elements['btn'].value =
    document.forms['formName'].elements['b'].length;
    ---
    if the input elements are in the form with id="formName", or:
    ---
    document.getElementById("btn").value =
    document.getElementsByName("b").length;
    ---
    if they are not inside a form (and the "btn" element has "btn" as
    an "id" instead of a "name", which is appropriate for a unique
    identifier).

    > What are the tags that can be use as an array of tags by assigning
    > same name to these tags?


    Form controls (input, button, textarea, select, and object). They
    have name attributes, and there are allowed to be more than one
    with the same name.

    > <HTML><script language="JavaScript">


    The "language" attribute is deprecated, and the "type" attribute is required,
    in HTML 4, so the script tag should be:
    <script type="text/javascript">

    Script elements must be inside either the <head> or <body> elements.
    This one is placed before "<HEAD>", which is incorrect (yeah, browsers
    accept it, but again, incorrect HTML makes scripts unpredictable)

    > function checkButtonSets(){
    > //document.all.btn.value=document.all.t.length
    > document.all.btn.value=document.all.b.length


    document.getElementById("btn").value = // see below for id="btn"
    document.getElementsByName("b").length;

    > }</script><HEAD>
    >
    > <BODY>
    > <p name="t" id=q></p>
    > <p name="t" id=q></p>


    The "id" attributes must have unique values, so you can't call both of
    them "q".

    > <input type="Radio" name="b" value="a2">a2
    > <input type="Radio" name="b" value="a3">a3<br>
    > <input name="btn" type="Button" value="Check Button Sets"


    I'd give this input element an id="btn" instead of the "name" attribute,
    and then use the line above.

    > onClick="checkButtonSets()">
    > </BODY></HTML>


    Good luck!
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Oct 4, 2004
    #2
    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. Robbie Hatley
    Replies:
    6
    Views:
    408
    Richard Herring
    Jun 28, 2006
  2. Replies:
    2
    Views:
    255
    Thomas J. Gritzan
    Feb 13, 2007
  3. Seguros Catatumbo

    Sending several input tags with the same name

    Seguros Catatumbo, Sep 5, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    340
    Mark Rae [MVP]
    Sep 5, 2007
  4. weston
    Replies:
    19
    Views:
    217
  5. weston
    Replies:
    1
    Views:
    239
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page