Connecting Tech Pros Worldwide Help | Site Map

OnKeyUp event does not work

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 11:12 AM
Evan Wong
Guest
 
Posts: n/a
Default OnKeyUp event does not work

I have problem to get onkeyup event. If we put the event in HTML
statement like -

"<input name=field1 size=16 onkeyup="javascript:function1();>"

it works.

If we put in JavaScript code like this
var oElmt = document.createElement("input");
:
oElmt.onkeyup = function () {javascript:function1() };
or
oElmt.onkeyup = "javascript:function1()";

It will work sometimes, and failed sometimes. Most of the time I need
to switch to another field and back, to make the event working.

Anybody experienced this problem before?






*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

  #2  
Old July 23rd, 2005, 11:12 AM
Grant Wagner
Guest
 
Posts: n/a
Default Re: OnKeyUp event does not work

Evan Wong wrote:
[color=blue]
> I have problem to get onkeyup event. If we put the event in HTML
> statement like -
>
> "<input name=field1 size=16 onkeyup="javascript:function1();>"
>
> it works.
>
> If we put in JavaScript code like this
> var oElmt = document.createElement("input");
> :
> oElmt.onkeyup = function () {javascript:function1() };
> or
> oElmt.onkeyup = "javascript:function1()";
>
> It will work sometimes, and failed sometimes. Most of the time I need
> to switch to another field and back, to make the event working.
>
> Anybody experienced this problem before?[/color]

Yes, when you try to assign a string to what is a function reference, it
tends not to work too well. Use:

<body onload="test();">
<form name="myFormName" id="myFormId">
</form>
<script type="text/javascript">
function test() {
var a = document.createElement('input');
// assign the reference to function1 to the onkeyup event
a.onkeyup = function1;
document.forms['myFormName'].appendChild(a);
// or document.getElementById('myFormId').appendChild(a) ;
}
function function1() {
alert('keyup');
}
</script>
</body>

Just as a side note:

oElmt.onkeyup = function() { javascript:function1(); };

should work, but "javascript:" in that example is simply a label that
serves no purpose, and you lose the ability to refer to have "this" in
function1() refer to the element.

--
| Grant Wagner <gwagner@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html


  #3  
Old July 23rd, 2005, 11:13 AM
Evan Wong
Guest
 
Posts: n/a
Default Re: OnKeyUp event does not work

My findings on this problem. Actually the original code works.
However, when the screen is loaded, my code will set focus on that text
box that has onkeyup event set. If I type in the text right away, the
onkeyup event is not fired. If I click on the text box (the same text
box), the onkeyup event is fired and the function1 will be executed for
every keystroke.

I have tried to use .focus() and .select() to set focus on the field,
and the behavior is the same. Any idea?



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.