I have a few rows with text, which I can "edit" by replacing the row inline with an input, which gets the text from the row as its value attribute.
When the input looses focus, the previous text is restored.
When pressing return (keyCode 13), the new text is saved and will be written in the row.
Now, for the users who don't know to press return for saving the text, I want to add a "Save" button right next to the input field. But when pressing it, the blur event of the input field is getting fired first, discarding the changes.
So is there an easy way, that the .click() event of the button can cancel the .blur() event on the input field? Perhaps an "don't execute other events" or can I perhaps see in the blur event, which events will get called next to cancel it?
Here is a jsfiddle to see what I mean: http://jsfiddle.net/ykY5X/ (I'm working in Firefox (latest Nightly), where the button click won't work. I just tested the jsfiddle also in chrome, where also the keyup won't work as expected.)
$('#showEdit').click(function() {
$('#row').data('text',$('#row').text());
$('#row').html('<input type="text" id="editInput" /> <span id="editSave">Save</span>');
$('#editInput').val($('#row').data('text')).focus();
});
$('#row').delegate('#editInput','keyup',function(e) {
e.stopPropagation();
var keycode = e.keyCode || e.which;
if (keycode == 13) $('#editSave').click();
}).delegate('#editSave','click',function(e) {
e.stopPropagation();
$('#row').text($('#editInput').val());
}).delegate('#editInput','blur',function() {
$('#row').text($('#row').data('text'));
});
click
event won't fire, as soon as theblur
event removes the button. It only fires themousedown
event of the button. With a bit of temporary data, it works nice :) Thank you! – Wulf