html - Shifting focus in JQuery not working -
<input type="text" id="title" value="write post..." /> <input type="button" id="save_post" class="button" value="submit" style="cursor:pointer;"/>
so binded enter key on keyboard something, here is:
$(document).keypress(function(e){ if (e.which == 13){ $("#save_post").click(); $("#save_post").focus(); } });
problem is, want shift focus if clicking button, takes focus away text field (in have character counter for), , takes cursor out of text field, isn't working. cursor stays in field , character counter still counts submitted though nothing in textbox anymore.
what doing wrong?
the following worked me:
/* cache reference save_post since use more once */ var $btn_savepost = $("#save_post"); /* make sure know when our button hit */ $btn_savepost.click(function(){ alert("clicked me"); }); /* capture kepress events on document level, , check key */ $(document).keypress(function(e){ if ( e.which === 13 ) $btn_savepost.focus().click(); });
Comments
Post a Comment