David Dr.
David Dr.

Reputation: 87

Javascript change img src not working

I have a simple img without a source specified so that I can use it with javascript. But for some reason, it doesn't work.

The img with id "recipes" is what I want to change according to the image clicked. The only image I have for testing is the first one after the paragraph with src "images/BTNsmoki.jpg" but that doesn't work so I can't apply the same to each other.

function showitemrecipes(b) {
  if (b == "items/BTNsmoki.jpg") {
    var ele = document.getElementById("element");
    ele.style.display = "none";
    document.getElementById('recipes').innerHTML = '<font color="#FFCC00">Smoki</font>';
    document.getElementById("itemrecept").src = b;
  } else
    document.getElementById('recipes').innerHTML = 'KUR';
}
<html>

<body>

  <div style="clear:both;"></div>
  <div align="center" style="background: #000000" class="cat">
    <h1 align="center"><font color="#6F249E">FORUM</font></h1>
    <h2 align="center"><font color="#D4A819">РЕЦЕПТИ</font></h2>

    <a href="#" onclick="showitemrecipes(ranec);">
      <img src="items/BTNranec1.jpg" width="40px" height="40px" border="1" />
    </a>
    <a href="#" onclick="showitemrecipes(lanec);">
      <img src="items/BTNlanec.jpg" width="40px" height="40px" border="1" />
    </a>
    <a href="#" onclick="showitemrecipes(vesnik);">
      <img src="items/BTNkniga.jpg" width="40px" height="40px" border="1" />
    </a>
    <a href="#" onclick="showitemrecipes(stap);">
      <img src="items/BTNstap.jpg" width="40px" height="40px" border="1" />
    </a>
    <p>
      <a href="#" onclick='showitemrecipes("items/BTNsmoki.jpg");'>
        <img src="items/BTNsmoki.jpg" width="40px" height="40px" border="1" />
      </a>
      <a href="#" onclick="showitemrecipes(sokce);">
        <img src="items/BTNsokce.jpg" width="40px" height="40px" border="1" />
      </a>
      <a href="#" onclick="showitemrecipes(bluzon);">
        <img src="items/BTNjakna3.jpg" width="40px" height="40px" border="1" />
      </a>
      <a href="#" onclick="showitemrecipes(kapce);">
        <img src="items/BTNkapce.jpg" width="40px" height="40px" border="1" />
      </a>
      <p>
        <a href="#" onclick="showitemrecipes(papuchi);">
          <img src="items/BTNpapuchi.jpg" width="40px" height="40px" border="1" />
        </a>
        <a href="#" onclick="showitemrecipes(rakavica);">
          <img src="items/BTNrakavica.jpg" width="40px" height="40px" border="1" />
        </a>
        <a href="#" onclick="showitemrecipes(nitnarica);">
          <img src="items/BTNnitnarica.jpg" width="40px" height="40px" border="1" />
        </a>

  </div>
  <div align="center" style="background: #000000" id="recipes">
    <font id="element" color="#FFCC00">Рецептите ќе се покажат овде</font>
    <p>
      <img id="itemrecept" />
  </div>
</body>

</html>

Can anyone tell me why?

Upvotes: 0

Views: 670

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

document.getElementById('recipes').innerHTML = ... is removing the element with id="itemrecept"

so document.getElementById("itemrecept") fails miserably

Upvotes: 2

Related Questions