Bug Tracker

Modify

Ticket #10845 (closed bug: invalid)

Opened 19 months ago

Last modified 16 months ago

preventDefault in keypress event cancel change event

Reported by: rpalbert@… Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.1rc1
Keywords: Cc:
Blocking: Blocked by:

Description

If i return false from keypress event or do a preventDefault, the change event when i leave text field is not called.

here is a simple example:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WrongPreventDefault.aspx.cs" Inherits="TPUsineV2.WrongPreventDefault" %> <!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 TransitionalEN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=" http://www.w3.org/1999/xhtml"> <head runat="server">

<title></title> <script type="text/javascript" src="Scripts/jquery-1.7.min.js"></script> <script type="text/javascript" language="javascript">

$(document).ready(function () {

$("#TextBox1").on('keypress.datetime', function (event) {

$(this).val('i processed') return false;

}).on('change.datetime', function (event) {

alert('change');

});

});

</script>

</head> <body>

<form id="form1" runat="server"> <div>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

</div> </form>

</body> </html>

Change History

comment:1 Changed 19 months ago by dmethvin

  • Status changed from new to closed
  • Resolution set to invalid

I've visually compiled your example and tried to create a test case:

 http://jsfiddle.net/dmethvin/UpDLV/

From what I can tell though, this works as expected. Here's the W3C events spec:

 http://www.w3.org/TR/DOM-Level-3-Events/#event-type-keypress

The default action of the keypress event must be to dispatch a textinput event with the data attribute set to the value of the char attribute of the keypress event.

You've prevented the default action so the character typed by the user is not entered and there is no need for change event. You've also set the value *explicitly* in code using .val() but that does not fire a change event. Only user-initiated actions fire those events.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.