Jump to content

[SOLVED] Displaying a textbox value as typing + divide it by 5


slyte33

Recommended Posts

This is for my text-based rpg game.

 

So let's say i had this:

 


<form method="post" action="guild_payout.php">

<input type="text" name="amount" id="amount" onkeyup="showMsg()" value="" size="15"/>
<input type="submit" name="submit" value="Submit">

</form>

 

I have it to display the amount typed correctly, but what I want to do is divide what was typed by 5, but I can't seem to get it to work right.

 

I have this:

 

<script type="text/javascript">
function showMsg(){
  var userInput = document.getElementById('amount').value;
  document.getElementById('userMsg').innerHTML = userInput;
}
</script>
<div id=userMsg></div>

 

So it does display the amount typed, but ofcourse you can't divide a "div" tag by 5, correct?

If i'm wrong, then how  :shy:

 

But if im right, how else?

Any feedback is very much appreciated  :D

Link to comment
Share on other sites

Why cant you divide by 5? your right a div cant be used in math but your putting the value of the text box into a variable and as such you can do pritty much anything with it...

 

try the following

<script type="text/javascript">
function showMsg(){
  var userInput = document.getElementById('amount').value;
  document.getElementById('userMsg').innerHTML = userInput / 5;
}
</script>
<div id=userMsg></div>
<form method="post" action="guild_payout.php">

<input type="text" name="amount" id="amount" onkeyup="showMsg()" value="" size="15"/>
<input type="submit" name="submit" value="Submit">

</form>

 

what you will have todo is make sure that only numbers are entered and not text other wise js will return NaN

 

Stuie

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.