Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    199
    Thanks
    78
    Thanked 5 Times in 5 Posts

    Target children by tagname

    Hi guys I was wondering if its possible in javascript to do something like this:

    Code:
    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?

    Code:
    <!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>

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 592 Times in 572 Posts
    you need to loop and do it to each subtag.
    Code:
    var r=document.getElementById('div').getElementByTagName('img');
    for(var i=0, mx=r.length;i<mx;i++){
      r[i].style.border = "1px solid black";
    }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • Users who have thanked rnd me for this post:

    conware (07-19-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •