Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Jenny
Guest
 
Posts: n/a
#1: Jul 23 '05
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>

Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Jul 23 '05

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


jenny_jones_79@hotmail.com (Jenny) writes:
[color=blue]
> Can I create an array of tags by assigning same name to these tags?[/color]

If I understand your question correctly, then no, not in standard
compliant browsers.
[color=blue]
> For example, I have two <p> tags with the same name t1.[/color]

<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.
[color=blue]
> But
>
> document.all.b.value=document.all.t.length[/color]

"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.
[color=blue]
> does not work. It works if the tags are <input type=radio...>. This[/color]

Input elements have "name" attributes.
[color=blue]
> line is OK:
>
> document.all.btn.value=document.all.b.length[/color]

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).
[color=blue]
> What are the tags that can be use as an array of tags by assigning
> same name to these tags?[/color]

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.
[color=blue]
> <HTML><script language="JavaScript">[/color]

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)
[color=blue]
> function checkButtonSets(){
> //document.all.btn.value=document.all.t.length
> document.all.btn.value=document.all.b.length[/color]

document.getElementById("btn").value = // see below for id="btn"
document.getElementsByName("b").length;
[color=blue]
> }</script><HEAD>
>
> <BODY>
> <p name="t" id=q></p>
> <p name="t" id=q></p>[/color]

The "id" attributes must have unique values, so you can't call both of
them "q".
[color=blue]
> <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"[/color]

I'd give this input element an id="btn" instead of the "name" attribute,
and then use the line above.
[color=blue]
> onClick="checkButtonSets()">
> </BODY></HTML>[/color]

Good luck!
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread