jquery - How to assign enter key the same function as click? -
this question has answer here:
i'm trying have enter key perform same task when user clicks button (see jquery). however, when tried "keyup" , other event handlers, couldn't working correctly. ideas on simpliest way this?
thanks patience, , help.
see fiddle: https://jsfiddle.net/lightyearsaway/qg8f3q37/3/
html
<h3>needs</h3> <input type="text" id="need-input" placeholder="enter text"> <button id="btn1">need it!</button> <ul id="need-list"> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> </ul>
js
$(document).ready(function(){ // click button, add list $("#btn1").click(function(){ $("#need-list").append($("<li>", { text: $("#need-input").val() })); }); // click list item hide $('ul').on('click', 'li', function(event) { $(this).slideup(); }); });
try this:
$("#need-input").on("keydown", function(e) { if(e.keycode == 13) $("#btn1").click() });
Comments
Post a Comment