javascript - Firefox onkeypress event not passing in event parameter - Stack Overflow

I have the following textarea HTML tag that I am inserting a runtime into the DOM (I have only shown ho

I have the following textarea HTML tag that I am inserting a runtime into the DOM (I have only shown how it looks after it is inserted) and I'm set the onkeypress event to fire as an inline attribute. However, in Firefox I get an "event is not defined" error when I press a key went it is in focus.

<textarea rows="3" cols="70" type="text" name="resultsRename" value="" class="redlining_textContent_Dialog_textbox" dojoType="dijit.forms.SimpleTextArea" id="labelText" propercase="false" style="width:auto;" onkeypress="return handleEnterKey(event,doFunc();">

function handleEnterKey(e, func)
{
    //alert("hit key handler");
    //alert(dojo.toJson(e));
    var key = e.keyCode || e.which;
    if (key == 13)
    {
        func();
        return false;
    }
    else
    {
        return true;
    }

}

I have looked at tons of help telling me that I should expect an event object parameter to be available to the js code I place in the onkeypress attr, but I keep getting this error.

The textarea tag is not inside a pair of form tags (I saw somewhere that this can be an issue sometimes but I have no choice here).

I have tried a handleEnterKey function with only the e parameter but this makes no difference.

If I change the inline attribute to...

onkeypress="return handleEnterKey(this,doFunc();"

... I enter the handleEnterKey with my e parameter set to the textarea object. Why Am I not being passed the event object? Obviously this works fine in IE with it's global event object.

At my wits end. Help me Stack Overflow... you're my only hope!

I have the following textarea HTML tag that I am inserting a runtime into the DOM (I have only shown how it looks after it is inserted) and I'm set the onkeypress event to fire as an inline attribute. However, in Firefox I get an "event is not defined" error when I press a key went it is in focus.

<textarea rows="3" cols="70" type="text" name="resultsRename" value="" class="redlining_textContent_Dialog_textbox" dojoType="dijit.forms.SimpleTextArea" id="labelText" propercase="false" style="width:auto;" onkeypress="return handleEnterKey(event,doFunc();">

function handleEnterKey(e, func)
{
    //alert("hit key handler");
    //alert(dojo.toJson(e));
    var key = e.keyCode || e.which;
    if (key == 13)
    {
        func();
        return false;
    }
    else
    {
        return true;
    }

}

I have looked at tons of help telling me that I should expect an event object parameter to be available to the js code I place in the onkeypress attr, but I keep getting this error.

The textarea tag is not inside a pair of form tags (I saw somewhere that this can be an issue sometimes but I have no choice here).

I have tried a handleEnterKey function with only the e parameter but this makes no difference.

If I change the inline attribute to...

onkeypress="return handleEnterKey(this,doFunc();"

... I enter the handleEnterKey with my e parameter set to the textarea object. Why Am I not being passed the event object? Obviously this works fine in IE with it's global event object.

At my wits end. Help me Stack Overflow... you're my only hope!

Share Improve this question edited Jun 24, 2021 at 11:07 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 5, 2011 at 15:13 MrGFunkMrGFunk 631 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

handleEnterKey should not even be called as you have a syntax error.

return handleEnterKey(event,doFunc();

should be

return handleEnterKey(event,doFunc);

Passing event definitely works: DEMO

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745576305a4633983.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信