How to catch an Enter press in the same time working with blur() in jQuery -


it's to-do list column want link , when clicked on converted in textbox. want catch when user done, when user press enter or click somewhere (blur jquery) finish.

demo on jsfiddle

html

<li style="list-style:none" id="add">add</a></li> <input type="text" style="input:focus; outline:none" name="task-group" display:none> 

jquery

    $(document).ready(function(){     $("input[name=task-group]").hide();      var addbtn = $("#add");     var inputbar = $("input[name=task-group]");      $("#add").click(function(){         $(this).hide();         $(inputbar).show().focus();     });         /*  $(document).keypress(function(e) {     if(e.which == 13 && $(inputbar).val() !== ""){                 $(inputbar).hide();                 var new_task_group  = $(inputbar).val();                 $(addbtn).text(new_task_group);                 $(addbtn).show();                     }                     else if(e.which !== 13){                         $(addbtn).show();                         $(inputbar).hide();                     }     });*/  $("input[name=task-group]").blur(function(){         if($(inputbar).val() !== ""){         $(this).hide();         var new_task_group  = $(this).val();         $(addbtn).text(new_task_group);         $(addbtn).show();         } // if close          else {             $(addbtn).show();             $(inputbar).hide();         }     });        }); 

usually,

you can use .on() bind function multiple events:

$('#element').on('keyup keypress blur change', function() {     ... }); 

or pass function parameter normal event functions:

var myfunction = function() {    ... }  $('#element')     .keyup(myfunction)     .keypress(myfunction)     .blur(myfunction)     .change(myfunction) 

but,

i don't know how check while binding, if user pressed enter. need in function, don't know if function instantiated due keypress or blur, did was, on pressing enter, blur() box, function automatically triggered on pressing enter too.

demo: your fiddle.

$("input[name=task-group]").keydown(function (e){ if(e.keycode == 13){     $("input[name=task-group]").blur(); } }); 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -