How to access child div elements under a given condition with javascript?
Posted
by hlovdal
on Stack Overflow
See other posts from Stack Overflow
or by hlovdal
Published on 2010-05-16T02:03:30Z
Indexed on
2010/05/16
2:10 UTC
Read the original article
Hit count: 252
My main question is to calculate the last alert message, but any other information is also welcome.
I am trying to learn javascript (to use with greasemonkey later), but I am struggling a bit to grasp the DOM and how to process it.
<html>
<head>
<script type="text/javascript">
function my_test()
{
var elements = document.getElementsByTagName("div");
// prints "found [object HTMLCollection] with length 8"
alert("found " + elements + " with length " + elements.length);
// prints "0:[object HTMLDivElement]"
alert("0:" + elements[0]);
// how to calculate the following?
alert("for intereting one is AAAA and three is CCCC");
}
</script>
</head>
<body>
<div class="interesing">
<div class="one">AAAA</div>
<div class="two">BBBB</div>
<div class="three">CCCC</div>
</div>
<div class="boring">
<div class="one">1111</div>
<div class="two">2222</div>
<div class="three">3333</div>
</div>
<input type="button" onclick="my_test()" value="my test"
</body>
</html>
So elements
is now an array of elements and I can access each of them individually. But where can I find what methods/properties these elements have?
© Stack Overflow or respective owner