2

It should be straight forward but I can not get a function to change the image on button click. Please can anyone spot what is wrong with the below code. Thanks in advance.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>BlankCordovaApp4</title>

<!-- BlankCordovaApp4 references -->
<link href="css/index.css" rel="stylesheet" />
</head>
<body>
<p>Greeting!</p>

<img id="demo" src="images/hello.jpg" width="180" height="123">

<button id="Btn" type="button" onclick="changeImage();">Click Me</button>

<!-- Cordova reference, this is added to your app when it's built. -->
<script src="cordova.js"></script>

<script src="scripts/platformOverrides.js"></script>

<script src="scripts/index.js"></script>


<script>

function changeImage() {

document.getelementbyid("demo").src = "images/bye.jpg";
};

</script>

</body>
</html>
3
  • You need to use getElementById('demo') This function is case sensitive Commented Feb 22, 2015 at 16:48
  • learn how to debug JavaScript using browser's inspect element or tools like firebug Commented Feb 22, 2015 at 16:55
  • Many thanks Jackson, changing the case fixed this Commented Feb 22, 2015 at 17:46

1 Answer 1

3

It should be:

document.getElementById("demo").src = "images/bye.jpg";

Case matters in javascript: getelementbyid is not the same as getElementById.

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

1 Comment

@HarryBains you can see javascript errors in a console. hit F12 on most browsers.

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.