开发者

Can't call keyup function of js object

开发者 https://www.devze.com 2023-02-23 04:17 出处:网络
Here is code http://jsfiddle.net/8ZzER/26/ I try to call the function bound to keyup but keyPres variable is null

Here is code http://jsfiddle.net/8ZzER/26/

I try to call the function bound to keyup but keyPres variable is null

HTML

<inp开发者_StackOverflow中文版ut id="text" name="text" type="text" onkeyup="alert('Boom')"  
       value="Key up here"/>
<input type="button" id="fire" value="fire" />

JavaScript

document.getElementById("fire").onclick=function()
{
    var textElement = document.getElementById("fire");
    var keyPres= textElement.onkeyup;
    keyPres.call();
}


Your handler is on the element with "id" value "text", but your code looks for "fire".

<input id="text" name="text" type="text" onkeyup="alert('Boom')"  
       value="Key up here"/>
<input type="button" id="fire" value="fire" />

The handler should look like:

// says "fire" in your jsFiddle
var textElement = document.getElementById("text"); 
var keyPres= textElement.onkeyup;
keyPres.call();


Well, you are assigning textElement to your button (fire) and not your text box.

http://jsfiddle.net/8ZzER/29/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号