0

I've been searching and trying to figure this out for hours but i seem to be missing something. Basically i'm trying to make it so if i click a link/button it executes a script to increase or decrease the number by a specified amount in a function. (in this example its by 1)

The image files that im trying to display are supposed to change depending on the final result. but the image never changes.

any suggestions are greatly appreciated

<script type="text/javascript">

var p1hps = 20;
var p1hpimage

function p1p1Click() {
    p1hps = (p1hps +1);
    function p1health(){
    };
}

function p1health() {
    p1hpimage = "images/p1" + p1hps + ".png";
    document.getElementById('p1hp').src = p1health();
}
</script>




<body>
<a href="#" onclick="p1p1Click()"><img src="images/p1p1.png" width="165" height="87" alt=""></a>
<img src="images/p120.png" id="p1hp" width="324" height="252" alt="">
</body>
1
  • You're calling your p1health method incorrectly. Try just p1health(); Commented Oct 17, 2013 at 2:27

2 Answers 2

1

You need to invoke the p1health function inside p1p1Click, instead you were declaring another function with the name p1health inside p1p1Click.

Also the images src property, you need to assign the value of the variable p1hpimage instead of recursively calling the p1health method

var p1hps = 20;
function p1p1Click() {
    p1hps++;
    p1health();
}
function p1health() {
    var p1hpimage = "images/p1" + p1hps + ".png";
    document.getElementById('p1hp').src = p1hpimage ;
}

Demo: Fiddle -- inspect the source with dev tools to see the src getting updated

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

Comments

0

Try this :

var _myPicArray =new Array("pic1.png","pic2.png","pic3.png");

function p1health(n) {
    document.getElementById('p1hp').src = myPicArray[n] ;
}

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.