Search code examples
javascriptunused-variables

How to write the HTML code for the inline element showing an image for use in webpage?


I'm writing my first code in an interactive/follow-along program through Cengage (MindTap). The program is instructing me to "write the HTML code for the inline element showing the sky image to use in the webpage." I am supposed to create a variable named imgStr that stores this text string:

    <img src='sd_skyMap.png' />

Where Map is the value of the mapNum variable (there are 23 files titled sd_sky0, sd_sky1, sd_sky3 and so fourth). It says to use the + operator to combine text strings together and to include single-quote characters within the text strings.

I cannot get the sky images to appear on the webpage to save my life.

I've attempted going through a tutor provided through my university but have still have no luck getting the image to display.

    var imgStr = "<img src='sd_sky0 + sd_sky1 + sd_sky2 + sd_sky3 +
    sd_sky4 + sd_sky5 + sd_sky6 + sd_sky7 + sd_sky8 + sd_sky9 + 
    sd_sky10 + sd_sky11 + sd_sky12 + sd_sky13 + sd_sky14 + sd_sky15 
    + sd_sky16 + sd_sky17 + sd_sky18 + sd_sky19 + sd_sky20 + 
    sd_sky21 + sd_sky22 + sd_sky23' + mapNum + '.png' />";
    document.getElementById("planisphere").insertAdjacentHTML() = imgStr;

Having inserted the code into jshint.com, it stated one warning and one unused variable.

(Bad assignment.)

document.getElementById("planisphere").insertAdjacentHTML() = imgStr;

and mapNum is an unused variable.


Solution

  • InsertAdjacentHTML takes two strings as parameters.

    The first parameter is the position which takes one of four static values.

    The second parameter is your HTML string to be inserted.

    An example for what you want could be:

    document.getElementById("planisphere").insertAdjacentHTML('afterbegin', imgStr);