takayoshi
takayoshi

Reputation: 2799

Can't call keyup function of js object

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

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

HTML

<input 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();
}

Upvotes: 1

Views: 1864

Answers (2)

Pointy
Pointy

Reputation: 413757

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();

Upvotes: 3

Dutchie432
Dutchie432

Reputation: 29160

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

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

Upvotes: 1

Related Questions