0
<IMG onmouseover="document.swap2.src='http://www.grlf.com/pics/png';" id="brewmp" alt=Brew src=changeOSImage() width=26 height=24>


function changeOSImage() {
    var mp_os = "x";

    if (mp_os) == "Brew MP") {
        document.getElementById("brewmp").src = "http://www.greengo-cellular.com/ebay_files/images/features_n_02.png";
    } else {
        document.getElementById("brewmp").src = "http://www.greengo-cellular.com/ebay_files/images/features_02.png";
    }
};

For some reason instead of changing the url, the url displays the function name inside of it (therefore leading to nowhere). What have I done wrong?

2

3 Answers 3

3

You can not specify a function at the src attribute of an image.

Try putting the changeOSImage() function at the onload event of the document, or calling it from some other function.

Sign up to request clarification or add additional context in comments.

Comments

0

As already mentioned, the src of your image cannot be a function. Also I cannot see what document.swap2 actually refers to in your code. try something like

  <img id="brewmp" src="http://www.grlf.com/pics/png" />

  <script>
  window.onload = (function(){
         var mp_os = 'x';
         document.getElementById('brewmp').onmouseover = (function(){
              this.src = 'path/to/different/image';
         });
  });
  </script>

I'm not sure what mp_os is referring to in your initial code as it's set as x and you never change it, plus it is defined within the scope of your function which means it will always be 'x' in this case.The above should give you a good starting point to add your "if" statement into though, but you should declare var mp_os outside of the function

Comments

0

As noted in the other answers, you have an extra ) in your if statement. Try this:

Example: JsFiddle

JavaScript (added inside <head></head>):

<script>
var mp_os = '';

function changeImage(){

    if (mp_os == "Brew MP") {
        mp_os = "x";
        document.getElementById("brewmp").src = "http://png-5.findicons.com/files/icons/75/i_like_buttons_3a/512/perspective_button_stop.png";
    } else {
        mp_os = "Brew MP";
        document.getElementById("brewmp").src = "http://icons.iconarchive.com/icons/mazenl77/I-like-buttons-3a/512/Perspective-Button-Go-icon.png";
    }

}
</script>

HTML:

<img onmouseover="changeImage()" onmouseout="changeImage()" id="brewmp" alt="Brew" src="http://png-5.findicons.com/files/icons/75/i_like_buttons_3a/512/perspective_button_stop.png" width="20%">

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.