-1

I am trying to change an image in my HTML, via an If/else statement, so the page displays an image depending on a value from another script i currently have this code

<script>
      if (b.className="yes") {
    img src="Site/assets/HappyObama.jpg"
} 
else {
    img src="Site/assets/SadObama.jpg"
}   

how can i fix this? Is there something within Javascript that does this?

5
  • 1
    You're aware of the difference between assigning and comparing? Commented Mar 23, 2017 at 13:03
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Mar 23, 2017 at 13:04
  • Because of the way the project is already build (with if/else statements above) i'd like to keep to that otherwise, can you suggest a function or something? Commented Mar 23, 2017 at 13:05
  • 1
    Possible duplicate of Toggle image src attribute using Javascript Commented Mar 23, 2017 at 13:06
  • 2
    What is img supposed to represent in img src="Site/assets/HappyObama.jpg"? Commented Mar 23, 2017 at 13:06

4 Answers 4

-1

Here is working example

<!DOCTYPE html>
<html>
   <body>

     <p id="demo">Click the button to change the text in this paragraph.</p>
     <img id="img" src="#">

     <button onclick="myFunction()">Try it</button>

     <script>
     function myFunction() {
        document.getElementById("demo").innerHTML = "Hello World";
        var i = true;
        if(i) {
           document.getElementById("img").src = "https://www.w3schools.com/css/trolltunga.jpg";

        } else {
           document.getElementById("img").src = "http://wallpaper-         gallery.net/images/image/image-13.jpg";}
       }
    </script>

  </body>
 </html>
Sign up to request clarification or add additional context in comments.

Comments

-1

Use a function to set the value for src.

function getImage(className) { 

    var image = "";
    if (className == 'yes') {
        image = "Site/assets/HappyObama.jpg"
    } 
    else {
        image = "Site/assets/SadObama.jpg"
    }  
    return image;
}

Comments

-2

Assign an id to your img tag, then you can

//First using jQuery    
     <script>
              if (b.className="yes") 
              {
                 $("#img").attr('src', 'Site/assets/HappyObama.jpg'); 
              } 
              else 
              {
                $("#img").attr('src', 'Site/assets/SadObama.jpg');
              }   
            </script>

//Second using javascript
 <script>
     if (b.className="yes") 
     {
        document.getElementById("img").src="Site/assets/HappyObama.jpg";
     } 
     else 
     {
          document.getElementById("img").src="Site/assets/SadObama.jpg";
     }   
 </script>

Lemme know if the problem resolved. Happy to help

Comments

-5

You can do like this

if (b.className="yes") {
   document.getElementById("imageid").src="Site/assets/HappyObama.jpg";
} else {
   document.getElementById("imageid").src="Site/assets/SadObama.jpg";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.