Add an "onkeyup" event.

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Oct 2008
Posts: 15
Reputation: Neo7 is an unknown quantity at this point 
Solved Threads: 0
Neo7 Neo7 is offline Offline
Newbie Poster

Add an "onkeyup" event.

 
0
  #1
Aug 31st, 2009
Hi everybody,

What should I do in order to add an onkeyup="foo();" to an input box but without writing the onkeyup="foo();" inside the input field declaration?
Usually you do:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="text" name="hello" value="" class="input-tags" onkeyup="foo();"/>

But I want to do something like this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. document.getElementsByName("hello").item(0).addEventListener('keyup',foo(),true);
  3. </script>
and leave the input declaration like this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="text" name="hello" value="" class="input-tags"/>

Thanks for your time
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Add an "onkeyup" event.

 
1
  #2
Aug 31st, 2009
Hi,

here's a simple demonstration of installing events onto target elements':

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <meta http-equiv="Content-Script-Type" content="text/javascript">
  7. <title>Adding Events</title>
  8. <script type="text/javascript">
  9. <!--
  10. var addEvent = Object.prototype.addEvent = ( function( eve, fun ) {
  11. if ( "addEventListener" in window ) {
  12. this.addEventListener( eve, fun, false );
  13. } else {
  14. var thisEvent = "on" + eve;
  15. if ( "attachEvent" in window ) {
  16. this.attachEvent( thisEvent, fun );
  17. } else {
  18. this[ thisEvent ] = fun;
  19. }
  20. }
  21. } );
  22. // The addEvent function allows you to add any event on target element:
  23. // attaching onload event sample with window ( object ).
  24. window.addEvent( "load", ( function() {
  25. alert( "\nONLOAD event occured" );
  26. function whatEvent( e ) {
  27. var e = e ? e : event;
  28. alert( "\nON" + ( e.type ).toUpperCase() + " event occured" );
  29. }
  30. if ( "getElementById" in document ) {
  31. var div = document.getElementById("hello");
  32. var input = document.getElementById("world");
  33.  
  34. } else {
  35. var div = hello;
  36. var input = document.all.world;
  37. } div.addEvent( "click", whatEvent );
  38. input.addEvent( "keyup", whatEvent );
  39. } ));
  40.  
  41. //-->
  42. </script>
  43. </head>
  44. <body>
  45. <noscript>
  46. <p> This site requires a browser that support <b>JavaScript</b>. </p>
  47. </noscript>
  48. <div id="hello">OnClick</div>
  49. <div><br><br>
  50. <input type="text" id="world" name="world" value="OnKeyup" size="30">
  51. </div>
  52. </body>
  53. </html>
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 15
Reputation: Neo7 is an unknown quantity at this point 
Solved Threads: 0
Neo7 Neo7 is offline Offline
Newbie Poster

Re: Add an "onkeyup" event.

 
0
  #3
Aug 31st, 2009
It works!!
Thanks Essential for your help, I've already added it to your reputation

Just one more thing, do you know how to prevent ENTER from submitting?
I would like to add an event on a form's input field with your function that onkeypress (ENTER/CARRIAGE RETURN) don't submit the form.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Add an "onkeyup" event.

 
0
  #4
Sep 1st, 2009
Hi Neo,

try this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <meta http-equiv="Content-Script-Type" content="text/javascript">
  7. <title>www.daniweb.com</title>
  8. <script type="text/javascript">
  9. <!--
  10. var addEvent = Object.prototype.addEvent = ( function( eve, fun ) {
  11. if ( "addEventListener" in window ) {
  12. this.addEventListener( eve, fun, false );
  13. } else {
  14. var thisEvent = "on" + eve;
  15. if ( "attachEvent" in window ) {
  16. this.attachEvent( thisEvent, fun );
  17. } else {
  18. this[ thisEvent ] = fun;
  19. }
  20. }
  21. } );
  22.  
  23. //-->
  24. </script>
  25. </head>
  26. <body>
  27. <noscript>
  28. <p> This site requires a browser that support <b>JavaScript</b>. </p>
  29. </noscript>
  30. <div id="div">
  31. <form id="frm" name="frm" action="#">
  32. <div>Name: <input type="text" value="" size="20"> <input type="submit" value="submit"></div>
  33. </form>
  34. </div>
  35. <script type="text/javascript">
  36. <!--
  37.  
  38. var form = ( "frm" in document ) ? frm : document.getElementById("frm");
  39. form.addEvent( "submit", function( e ) {
  40. var e = e ? e : event;
  41. var keys = ( e.keyCode ) ? e.keyCode : e.which;
  42. if ( keys !== 13 ) { // if its not an enter key, then replace the action url of the form.
  43. form.action = "process.php";
  44. return;
  45. }
  46. } );
  47. //-->
  48. </script>
  49. </body>
  50. </html>
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 15
Reputation: Neo7 is an unknown quantity at this point 
Solved Threads: 0
Neo7 Neo7 is offline Offline
Newbie Poster

Re: Add an "onkeyup" event.

 
0
  #5
Sep 2nd, 2009
Hi Essential,
Thank you very much for your time and your code
But I'm sorry to say that this time it doesn't work for me
I'm still submitting the form.

Firstable, I copied your code to an empty web page and tried it but he form was submitted.
Then I tried to included it in the real page I would like to fix and after many changes and trials still didn't stop the form from submitting.

It is possible to just ignore the "enter key" in a single input field or it is necessary to block the whole form from submitting?

By the way, I'm using something like this code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var form = ( "form" in document ) ? form : document.getElementsByTagName("form").item(1);//second form tag=item(1)
in order to select the form because it doesn't have any "name" or "id".
I hope that is not the cause for not working.
Also here I left you some code of my webpage's form in case:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="register-box">
  2. <h2>Register</h2>
  3. <form action="http://mywebpage/action/register" method="POST" >
And an example of one or my input fields within the form:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <label>Username<br/>
  2. <input type="text" name="username" value="" class="general-textarea"/>
None of those can be change beforehand, the only way to select them and to modify its behavior it is with scripting.
Last edited by Neo7; Sep 2nd, 2009 at 5:17 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC