D
Derek Basch
I have a simple XHTML document that I would like to apply javascript
functionality to:
<body>
<div class="length">9</div>
<div class="sequence">AAAAAAATTTAAA</div>
<div class="prediction">1.68509 1.60743 1.32631 1.38815 1.24905</div>
<div class="length">5</div>
<div class="sequence">TTTAAAAAA</div>
<div class="prediction">2.4898</div>
<div class="length">7</div>
<div class="sequence">TTTAAASSSSAAA</div>
<div class="prediction">2.4898 5.625 2.589 5.287</div>
</body>
Say that I wanted to select all of the <div> tags of class "length" and
operate on them. My options are limited.
1)
If I wanted to use document.getElementsByTagName() I could make each
"length" class <div> tag into a different, distinct, tag. <h1> for
"length", <h2> for "sequence", etc.. This seems rigid and
non-descriptive of the nature of the element. Also, what happens when I
run out of <h*> tags to utilize?
2)
If I want to use document.getElementById() I need to ensure that each
"length" <div> has a unique "id" attribute (length_1, length_2, etc..).
Then, create an array of these elements and operate on them.
(ref.
http://groups-beta.google.com/group...frm/thread/1012a90fa6c82f96/8082854908132dea?.
)
This seems cumbersome and failure prone.
3)
Finally, I could give each "length" <div> tag an "id" attribute of
"length" and select them using document.getElementsByName(). This is
not valid XHTML. Quote from W3 validator:
"An "id" is a unique identifier. Each time this attribute is used in a
document it must have a different value. If you are using this
attribute as a hock for style sheets it may be more appropriate to use
classes (which group elements) than id (which are used to identify
exactly one element)."
Why is there no DOM method for selecting a class of elements? What is
my best strategy for dealing with this issue? Thanks everyone.
functionality to:
<body>
<div class="length">9</div>
<div class="sequence">AAAAAAATTTAAA</div>
<div class="prediction">1.68509 1.60743 1.32631 1.38815 1.24905</div>
<div class="length">5</div>
<div class="sequence">TTTAAAAAA</div>
<div class="prediction">2.4898</div>
<div class="length">7</div>
<div class="sequence">TTTAAASSSSAAA</div>
<div class="prediction">2.4898 5.625 2.589 5.287</div>
</body>
Say that I wanted to select all of the <div> tags of class "length" and
operate on them. My options are limited.
1)
If I wanted to use document.getElementsByTagName() I could make each
"length" class <div> tag into a different, distinct, tag. <h1> for
"length", <h2> for "sequence", etc.. This seems rigid and
non-descriptive of the nature of the element. Also, what happens when I
run out of <h*> tags to utilize?
2)
If I want to use document.getElementById() I need to ensure that each
"length" <div> has a unique "id" attribute (length_1, length_2, etc..).
Then, create an array of these elements and operate on them.
(ref.
http://groups-beta.google.com/group...frm/thread/1012a90fa6c82f96/8082854908132dea?.
)
This seems cumbersome and failure prone.
3)
Finally, I could give each "length" <div> tag an "id" attribute of
"length" and select them using document.getElementsByName(). This is
not valid XHTML. Quote from W3 validator:
"An "id" is a unique identifier. Each time this attribute is used in a
document it must have a different value. If you are using this
attribute as a hock for style sheets it may be more appropriate to use
classes (which group elements) than id (which are used to identify
exactly one element)."
Why is there no DOM method for selecting a class of elements? What is
my best strategy for dealing with this issue? Thanks everyone.