I have the following jQuery/js controlling a trello style input field. So when you click on the span element, it switches to an input field. Then after you finish editing the element and take focus away from it(blur), it switches back to the span element with the new text. I also having it submitting to an ajax script to submit the new text to my database.
Now this all works flawlessly and I have no problems with what is described above. The problem came when I tried to make the "switch back to the span element" work on "enter" key press.
I have tested the code with an alert so I know the enter key is being detected but I can not get the "editableTextBlurred" function to fire from within the keypress function. Which means that it will not switch back to the span element on enter press.
function divClicked(div) { div = "#"+div; var divHtml = $(div).text(); var editableText = $('<input type="text" id="firstname" name="firstname" placeholder="First Name" onChange="profileUpdate(this)">'); editableText.val(divHtml); $(div).replaceWith(editableText); editableText.focus(); console.log(editableText); // setup the blur event for this new textarea editableText.blur(editableTextBlurred); $(":input").keypress(function(event) { if (event.which == '13') { event.preventDefault(); editableTextBlurred(); } }); } function editableTextBlurred() { var html = $(this).val(); var viewableText = $("<span id='firtname_text' onClick='divClicked(this.id)'></span>"); viewableText.html(html); $(this).replaceWith(viewableText); } <span id='firtname_text' onClick='divClicked(this.id)'> ".ucfirst($fname)." </span> Any insight on what I missing or doing wrong will be greatly appreciated, Thanks!