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

J

Jenny

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

Lasse Reichstein Nielsen

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top