Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Can I create an array of tags by assigning same name?

Reply
Thread Tools

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

 
 
Jenny
Guest
Posts: n/a
 
      10-04-2004
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>
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      10-04-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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 - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning an array to another array using C's assignment operator Myth__Buster C Programming 13 02-26-2013 04:29 PM
Assigning an array to another array using C's assignment operator Myth__Buster C Programming 0 02-01-2013 12:05 AM
Problem with assigning 1D array to a 2D array bintom C++ 11 10-15-2012 05:40 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
Problem assigning an Array object to an Array-subclass object Richard Lionheart Ruby 27 05-04-2004 06:42 AM



Advertisments