| | |
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:
Solved Threads: 0
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:
But I want to do something like this:
and leave the input declaration like this:
Thanks for your time
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)
<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)
<script type="text/javascript"> document.getElementsByName("hello").item(0).addEventListener('keyup',foo(),true); </script>
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<input type="text" name="hello" value="" class="input-tags"/>
Thanks for your time
Hi,
here's a simple demonstration of installing events onto target elements':
here's a simple demonstration of installing events onto target elements':
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <title>Adding Events</title> <script type="text/javascript"> <!-- var addEvent = Object.prototype.addEvent = ( function( eve, fun ) { if ( "addEventListener" in window ) { this.addEventListener( eve, fun, false ); } else { var thisEvent = "on" + eve; if ( "attachEvent" in window ) { this.attachEvent( thisEvent, fun ); } else { this[ thisEvent ] = fun; } } } ); // The addEvent function allows you to add any event on target element: // attaching onload event sample with window ( object ). window.addEvent( "load", ( function() { alert( "\nONLOAD event occured" ); function whatEvent( e ) { var e = e ? e : event; alert( "\nON" + ( e.type ).toUpperCase() + " event occured" ); } if ( "getElementById" in document ) { var div = document.getElementById("hello"); var input = document.getElementById("world"); } else { var div = hello; var input = document.all.world; } div.addEvent( "click", whatEvent ); input.addEvent( "keyup", whatEvent ); } )); //--> </script> </head> <body> <noscript> <p> This site requires a browser that support <b>JavaScript</b>. </p> </noscript> <div id="hello">OnClick</div> <div><br><br> <input type="text" id="world" name="world" value="OnKeyup" size="30"> </div> </body> </html>
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Hi Neo,
try this:
try this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <title>www.daniweb.com</title> <script type="text/javascript"> <!-- var addEvent = Object.prototype.addEvent = ( function( eve, fun ) { if ( "addEventListener" in window ) { this.addEventListener( eve, fun, false ); } else { var thisEvent = "on" + eve; if ( "attachEvent" in window ) { this.attachEvent( thisEvent, fun ); } else { this[ thisEvent ] = fun; } } } ); //--> </script> </head> <body> <noscript> <p> This site requires a browser that support <b>JavaScript</b>. </p> </noscript> <div id="div"> <form id="frm" name="frm" action="#"> <div>Name: <input type="text" value="" size="20"> <input type="submit" value="submit"></div> </form> </div> <script type="text/javascript"> <!-- var form = ( "frm" in document ) ? frm : document.getElementById("frm"); form.addEvent( "submit", function( e ) { var e = e ? e : event; var keys = ( e.keyCode ) ? e.keyCode : e.which; if ( keys !== 13 ) { // if its not an enter key, then replace the action url of the form. form.action = "process.php"; return; } } ); //--> </script> </body> </html>
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
•
•
Join Date: Oct 2008
Posts: 15
Reputation:
Solved Threads: 0
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:
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:
And an example of one or my input fields within the form: None of those can be change beforehand, the only way to select them and to modify its behavior it is with scripting.
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)
var form = ( "form" in document ) ? form : document.getElementsByTagName("form").item(1);//second form tag=item(1)
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)
<div id="register-box"> <h2>Register</h2> <form action="http://mywebpage/action/register" method="POST" >
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<label>Username<br/> <input type="text" name="username" value="" class="general-textarea"/>
Last edited by Neo7; Sep 2nd, 2009 at 5:17 am.
![]() |
Similar Threads
- it gives "not initialised" on the Applet (Java)
- Creating a "Data" folder in my C: Drive using my Java program (Java)
- Quick question about "getLocation()" (Java)
- "add this site to your bookmarks" in firefox (JavaScript / DHTML / AJAX)
- javascript works in IE but not working in firefox (JavaScript / DHTML / AJAX)
- "topantispyware" virus (Viruses, Spyware and other Nasties)
- google "keyword" question (Search Engine Optimization)
- "enter" event (C#)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Javascript learning resources - for a javascript noobie
- Next Thread: Number of Visible items in DropDown List
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array browser bug captchaformproblem cart checkbox child class close codes createrange() css cursor date debugger decimal dependent design disablefirebug dom dropdown editor element embed engine enter error events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe images index internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl listbox maps masterpage math media menu mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php position post problem programming progressbar prototype redirect runtime safari scale scriptlets scroll search security shopping size software toggle unicode w3c web wysiwyg \n






