Search code examples
javascripthtmlimageurltarget

Display Image Using URL from GetElementByID


The code below is sending a URL of a image which I would like to display with the div ID being 'target', so far I can only display the actual URL address instead of the actual image.

            <script type='text/javascript'>
                function updateTarget( img ){
                    'use strict';
                    document.getElementById('target').innerHTML = img;
                }
            </script>

Target div

            <div id = 'target'> </div>

Any help would be appreciated as I am struggling to find any useful examples online.


Solution

  • Use this line:

    document.getElementById('target').innerHtml = '<img src="' +img+'"/>';