Nate Rice Nate Rice - 1 month ago 8
HTML Question

How to change the value in an onClick with javascript?

I have a clicker game that I am developing and when I click the image at the start I have it set by default to be one click per click. I am wanting to make a button that forces the value of one click, to be two clicks per click. None of the solutions I have tried seem to work. Here is what I have:

HTML:


<img src="rust.jpg" id="click" value="Click" />

</div>

<script src="picture.js"></script>

<script src="clicks.js"></script>

<script src="2clicks.js"></script>

<p>Clicks: <a id="clicks">0</a></p>

<button onClick="twoClicks()">Click Me</button>


Clicks.js:

var clicks = 0;
function onClick(x) {
clicks += x;
document.getElementById("clicks").innerHTML = clicks;
};


2clicks.js:

function twoClicks() {
document.getElementById("onClick").innerHTML = onClick(2);
}


I want to click the button and make the img increase clicks by two

Answer

Might be making this to hard on your self

Why not have a click method and have it take an argument of it's multiplier.

For example if you need on click than do something like this

click(1) and for two clicks do something like this click(2)

So for your game you could have code that looks like this in Javascript

function click(n){
   for(var i = 0; i < n; i++){
    addPoints()
   }
}

Or even just add points directly through from your click function.

All you would have to do for the different clicks would call them as such

<button id="clickOne" onClick="click(1)">Click Me</button>

<button id="clickTwo" onClick="click(2)">Click me for two</button>

And if you want these values to increase with outside influence you could even have a value for your multiplier. There is allot of ways to approach this and if you need more assistance let me know in the comments.

If you need to make the clickOne element do two clicks suddenly something you can do is reassign the onclick attribute like so.

document.getElementById('clickOne').onclick = function(){click(2)};