javascript - How to target image inside a div with an id with getelementbyid/tag/name? -
basically want change smiley.gif landscape.jpg without changing divs , tags, code not working. suggestions?
<div id="foo"> <div id="bar"><img src="smiley.gif"> hello world! </div> </div> <script> getelementbyid("bar").getelementsbytagname("img").src="landscape.jpg"; </script>
it better if assign id img tag , use this
<div id="foo"> <div id="bar"><img src="smiley.gif" id="myimg"> hello world! </div> </div> <script> document.getelementbyid("myimg").src="landscape.jpg"; </script>
or use this
<div id="foo"> <div id="bar"><img src="smiley.gif"> hello world! </div> </div> <script> var obj = document.getelementbyid("foo"); if(obj != null) { var images = obj.getelementsbytagname('img'); images[0].src = 'landscape.jpg'; } </script>
Comments
Post a Comment