I can use this to select all the anchor tags in a document, which works great...
var anchors = document.getElementsByTagName('a');
Specifically for replacing "href" functionality with "onclick" (for extending functionality from no javascript, adding thumbnails etc)
Except how can I get it to select just the anchor elements in a certain class of parent element (e.g. pics), leaving the bulk of anchors alone?
<a href="img/TEST/pretty.jpg">ignore me</a>
<div class="pics">
<a href="img/TEST/testPic1.jpg">1 stuff....</a>
<a href="img/TEST/testPic2.jpg">2 stuff...</a>
<a href="img/TEST/TestPic3.jpg">3 stuff..</a>
...
<a href="img/TEST/TestPicN.jpg">n stuff..</a>
</div>
...without additional libraries such as jQuery please!
Here's a quick demo...
function go() {
var anchorElements = document.getElementsByTagName('a');
for (var i in anchorElements)
anchorElements[i].style.backgroundColor = "red";
}
a{background-color:#0F0;}
<a href="img/TEST/pretty.jpg">ignore me</a>
<div class="pics">
<a href="img/TEST/testPic1.jpg">1 stuff....</a>
<a href="img/TEST/testPic2.jpg">2 stuff...</a>
<a href="img/TEST/TestPic3.jpg">3 stuff..</a>
...
<a href="img/TEST/TestPicN.jpg">n stuff..</a>
</div>
<button onclick="go()">Go!</button>
I know a workaround is just to select by a class applied to the elements that need modifying
document.links. ;-)