Hyperius
Hyperius

Reputation: 3

How to update an input field after multiplying it by two

How are you today? I am new to coding but am making my best effort. I am trying to build my own personal bitcoin casino website.

I am trying to take the value of an input field and then multiply that value by 2 by clicking a button that says 'X2'. Then I need the input field to display this new number so that the user can make a bet of that size.

I have tried everything I can think of over the last 3 days and am still unable to do this. Any and all help is very much appreciated. Thank you.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My Bitcoin Casino</title>
</head>

<body>
<button type="button" onclick="MultiplyWagerFunction()">X2</button>

<form>
<input type="text" id="wagerBox" value="0.00000001"></form>

<script>
function MultiplyWagerFunction() {
var wager = document.getElementById("wagerBox").value;
var wagerUpdate = wager*2;
oFormObject.elements["wagerBox"].value = 'wagerUpdate';
}
</script>
</body>
</html>

Upvotes: 0

Views: 96

Answers (5)

adsaro
adsaro

Reputation: 1

Maybe this is not exactly what you need, but if you use jquery you could do this

MultiplyWagerFunction(){
    var box = $("#wagerBox");
    box.val(box.val()*2);
}

Here box is defined to contain the wagerBox element and then the function val() is used in two different ways, it takes the value in the box, multiply it times 2 and the result is used to set the new value of the box.

Or if you want to be a little more verbose you also could do

MultiplyWagerFunction(){
    var box = $("#wagerBox");
    var newValue = box.val()*2;
    box.val(newValue);
}

Hope this could be useful.

Upvotes: 0

Jerry M
Jerry M

Reputation: 332

document.getElementById("wagerBox").value = 10;

This way you can set value of this input to 10.

Personally I recommend you to do this this way:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <input type="text" id="wage"/>
        <input type="button" id="timesTwo" value="X2"/>
        <script>
            var wageInput = document.getElementById("wage");
            var button = document.getElementById("timesTwo");
            button.addEventListener("click", function(){
                wageInput.value = parseInt(wageInput.value) * 2;
            });
        </script>
    </body>
</html>

There is full explanation about how "addEventListener" works: link

There is about parseInt() function: link

If you got any questions don't be afraid to ask in comments.

Upvotes: 1

Pankaj Shukla
Pankaj Shukla

Reputation: 2672

You need to do this:

The problem was that you were trying to modify the value with "wagerUpdate" Also, you need to assign the value to the same input control which in below code is elem.

function MultiplyWagerFunction() {
            var elem= document.getElementById("wagerBox");
            var wagerUpdate = elem.value * 2;
            elem.value = wagerUpdate;
    }

Upvotes: 0

Freddy Ochner
Freddy Ochner

Reputation: 165

this will work

<body>
<button type="button" onclick="MultiplyWagerFunction()">X2</button>

<form>
<input type="text" id="wagerBox" value="0.000001">
</form>

<script>
function MultiplyWagerFunction() {
var wager = document.getElementById("wagerBox");
var wagerValue = wager.value;
var wagerUpdate =  wagerValue * 2;
wager.value = wagerUpdate;
}
</script>
</body>

Freddy

Upvotes: 0

Sterling Beason
Sterling Beason

Reputation: 630

Your var wagerUpdate needs to be called without the quotes. See below oFormObject.elements["wagerBox"].value = wagerUpdate;

Upvotes: 0

Related Questions