Jump to content

Change input field onkeyup


Guest FirefoxRocks

Recommended Posts

Guest FirefoxRocks

Doesn't work for me. Maybe I have a wrong function?

<script type="text/javascript">function change(){var c=document.getElementById("pages").value/3;document.getElementById.("credits").value=Math.round(c)}</script>

The input buttons:

<td><input type="text" size="2" maxlength="2" id="pages" name="pages" onkeydown="change()" />|--> It will cost you  <input type="text" size="2" maxlength="2" id="credits" name="credits" /> credits.</td>

I tried putting the onkeydown="change()" into different input also and tried a different event. No luck. Help is needed :|

Link to comment
Share on other sites

If you are using onkeydown event, then the first time when a number is pressed, the value in textbox is null. instead use onkeyup event. try code below.

<script type="text/javascript">function change(num){var c;c=num/3;document.getElementById("credits").value=Math.round(c)}</script><input type="text" size="2" maxlength="2" id="pages" name="pages" onkeyup="change(this.value)" />|--> It will cost you <input type="text" size="2" maxlength="2" id="credits" name="credits" />

Link to comment
Share on other sites

Guest FirefoxRocks
If you are using onkeydown event, then the first time when a number is pressed, the value in textbox is null. instead use onkeyup event. try code below.
<script type="text/javascript">function change(num){var c;c=num/3;document.getElementById("credits").value=Math.round(c)}</script><input type="text" size="2" maxlength="2" id="pages" name="pages" onkeyup="change(this.value)" />|--> It will cost you <input type="text" size="2" maxlength="2" id="credits" name="credits" />

It works! Thank you very much! As you can see, I'm not good at JavaScript :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...