943,683 Members | Top Members by Rank

Ad:
May 23rd, 2008
0

Javascript change text box fields

Expand Post »
Hi guys,

I am trying to make a javascript function that will add "000" to the end of a textbox value if the user presses k and also add "000000" if they press m. So i don't want the k or m to appear in the textbox and some 0's to appear instead. If anyone could help me out with some code id be very grateful.

Thanks everyone

This is where I am at now (Ive done the easy bit, cant find out about detecting if k or m is pressed)
javascript Syntax (Toggle Plain Text)
  1. function addZero(x) {
  2. if//I dont know how to find out if it is a k or m
  3. {
  4. x.value += 000;
  5. }
  6. }
Last edited by peter_budo; May 24th, 2008 at 7:57 pm. Reason: Corecting closing tag
Similar Threads
Reputation Points: 11
Solved Threads: 6
Junior Poster in Training
hooray is offline Offline
62 posts
since Jan 2008
May 24th, 2008
0

Re: Javascript change text box fields

A simple script like this should do the job. Do keep it mind that it can be made better in a lot of ways but for your purpose it should serve well enough:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv"Script-Content-Type" content="text/javascript">
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <meta http-equiv="Expires" content="0"> <!-- disable caching -->
  8. <title>Example</title>
  9. <script type="text/javascript">
  10. function handleAutoAppend(e) {
  11. // A mapping which maintains a list of what needs to be appended for
  12. // which key pressed by the user.
  13. var mappings = {k : "000", m: "000000"};
  14.  
  15. // Process the event triggered by the user and grab hold of the element
  16. // on which it was triggered along with the pressed keys' value
  17. e = e || window.event;
  18. var code = e.which || e.keyCode;
  19. var key = String.fromCharCode(code);
  20. var elem = e.srcElement || e.target;
  21.  
  22. // For each 'value' of our interest, append the requisite string at the
  23. // end of the existing text and return(false) so as not the render 'k'
  24. // or 'm'
  25. for(var k in mappings) {
  26. if(key == k) {
  27. var val = mappings[k];
  28. if(elem) {
  29. elem.value = elem.value + val;
  30. return(false);
  31. }
  32. }
  33. }
  34.  
  35. // For all other keys, don't do anything; allow the default behavior
  36. // to take over.
  37. return(true);
  38. }
  39. </script>
  40. </head>
  41. <body>
  42. <form id="frm" name="frm" action="#">
  43. <p>Enter something:</p>
  44. <input type="text" name="txt" id="txt" onkeypress="return handleAutoAppend(event);">
  45. </form>
  46. </body>
  47. </html>
Last edited by ~s.o.s~; May 24th, 2008 at 3:28 pm.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jul 10th, 2008
0

Re: Javascript change text box fields

sorry for the late reply, but this worked perfectly. Thanks
Reputation Points: 11
Solved Threads: 6
Junior Poster in Training
hooray is offline Offline
62 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: correct?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Mail form Subject Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC