By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

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

P: n/a
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>
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
je************@hotmail.com (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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.