I have this code in jquery:
$(document).ready(function () {
$("#main-edit").click( function() {
var cursorExists = $("#cursor").length;
if (!cursorExists){
$("#cursor-start").append("<input type='text' id = 'cursor' />");
$("#cursor").markCursor();
}
if (cursorExists){
$("#cursor").markCursor();
}
});
jQuery.fn.enterText = function(){
if( $("#cursor").val() ){
var character = $("#cursor").val();
$("#cursor").val("");
return this.append("<span>"+character+"</span>");
}
};
jQuery.fn.markCursor = function(){
$(this).focus();
$(this).keydown(function() {
$("#cursor-start").enterText();
});
};
});
My problem is in the enterText function. On the keydown function, I get the value of the input, store it, clear the input, and append the stored value. But when I type something like “foo”….I get “fo”…..when I type “food”….I get “foo”. So its not getting the last entered value for some reason.
Use keyup() instead of keydown()