943,917 Members | Top Members by Rank

Ad:
Aug 31st, 2009
0

Add an "onkeyup" event.

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Neo7 is offline Offline
18 posts
since Oct 2008
Aug 31st, 2009
1

Re: Add an "onkeyup" event.

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>
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Aug 31st, 2009
0

Re: Add an "onkeyup" event.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Neo7 is offline Offline
18 posts
since Oct 2008
Sep 1st, 2009
0

Re: Add an "onkeyup" event.

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>
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Sep 2nd, 2009
0

Re: Add an "onkeyup" event.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Neo7 is offline Offline
18 posts
since Oct 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: Javascript learning resources - for a javascript noobie
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Number of Visible items in DropDown List





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


Follow us on Twitter


© 2011 DaniWeb® LLC