mercredi 3 mars 2021

How to wait for user to press enter key in J-Query Text field?

I want to add validation on my text field that is, after filling text field user will press enter key and that text should be added in list.and if text field is empty or user press wrong key it will show and error in any form. But during typing is starts showing error not waiting for user to press enter key.

<input id="input" type="text" placeholder="Enter your Text" />
<button id="enterBtn">Enter</button>
<ul id="todos">
  <li>Cricket</li>
  <li>CC</li>
  <li>Web</li>
</ul>

$(function(){
$("#enterBtn").click(handleBindingsOnClick);
$("#input").keyup(handleBindingsOnPress);
});
function handleBindings(){
var newtodo = $("#input").val();
$("#todos").append("<li>"+ newtodo +"</li>");
$("#input").val("");
}
function handleBindingsOnClick(){
if($("#input").val() != ""){
    handleBindings();
}
}
function handleBindingsOnPress(event){
if(event.keyCode === 13 && $("#input").val() != ""){
    handleBindings();
}
else{
    alert("Please press enter key or fill the field");
}
}



Aucun commentaire:

Enregistrer un commentaire