Reputation: 79
I have a function that generates a random number for me between 2 values I set myself. Here's the code.
<script>
function coinsEarned(){
var worth = document.getElementById("Earned");
var coins=Math.floor(Math.random()*10000+1000);
Earned.value = coins;
}
</script>
This works perfect. Now, I have a text bubble that represents the users balance, It is also perfect for me. Here's the code.
<input class="balance" id="Balance" type=text" value="20000" readonly/>
I would like to add a new button that when clicked will transfer the generated number found above ^^ onto the balance text which is just stated there ^. How would I get this to happen using a JavaScript function preferably so when the button is clicked I will have onclick="funtion();" that will add the generated number to the current balance and then display a new balance.
Thanks in advance for help :)
Upvotes: 0
Views: 135
Reputation: 251132
You can access the current amount using the value
attribute.
function coinsEarned(){
var current = document.getElementById("Balance");
var coins = Math.floor(Math.random()*10000+1000);
current.value = +current.value + coins;
}
document.getElementById("MyButton").onclick = coinsEarned;
Upvotes: 2