...

View Full Version : Target children by tagname



conware
07-19-2011, 05:42 PM
Hi guys I was wondering if its possible in javascript to do something like this:


document.getElementById('div').children('img').style

This is my code with int value I can target children is this also possible with a string like img?



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-gb" xml:lang="en-gb">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

<script type="text/javascript">
window.onload=function()
{
var d = document;
var e = d.getElementById('t');

e.children[0].style.border = "solid black 1px";
}
</script>

</head>
<body>

<div id="t">
<img width="120" height="80" src="./images/full6.jpg" alt="" />
<img width="120" height="80" src="./images/full6.jpg" alt="" />
<img width="120" height="80" src="./images/full6.jpg" alt="" />
<img width="120" height="80" src="./images/full6.jpg" alt="" />
</div>

</body>
</html>

rnd me
07-19-2011, 05:53 PM
you need to loop and do it to each subtag.


var r=document.getElementById('div').getElementByTagName('img');
for(var i=0, mx=r.length;i<mx;i++){
r[i].style.border = "1px solid black";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum