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> 

jsfiddle1

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

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -