Best way to detect change in textbox and send typing/not typing 4 ajax
Hello,
I've already developed a system for doing this which isn't perfect i'm wondering if there is a better solution. Here is the code I got atm.
- $(".chatmsg").live("blur", function(event) { stopTyping(); });
- $(".chatmsg").live("focus", function() {
- if($(".chatmsg").val().length > 0) {
- typing();
- }
- });
- $(".chatmsg").live("keypress keydown", function(event) {
- var key = event.keyCode;
-
- if (key == "13") {
- event.preventDefault();
- sendMsg();
- }
-
- var msg = $(".chatmsg").val();
- if(key == "8" && msg.length == 1) {
- stopTyping();
- } else if ($.trim(msg) != "") {
- typing();
- } else {
- stopTyping();
- }
- });
the textbox chatmsg class is created dynamically so I have to use live for this.
Anyway to optimize this?
I don't like the use of key == 8 and length == 1 to detect that chatmsg box is empty, but the event triggers before the val() of the chatmsg actually changes. So is there nothing better I can do?
Thanks,
Igor