View Single Post
Join Date: Jun 2006
Posts: 7,621
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Javascript change text box fields

 
0
  #2
May 24th, 2008
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.
I don't accept change; I don't deserve to live.
Reply With Quote