简体   繁体   中英

jquery-Print user's input when user finishes typing instead of on key up?

I want to print the user's input when he stops typing. My html code is:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="jacoco.css">
  <script src="https://code.jquery.com/jquery-3.4.0.js" integrity="sha256-DYZMCC8HTC+QDr5QNaIcfR7VSPtcISykd+6eSmBW5qo=" crossorigin="anonymous"></script>
  <script src="jacoco.js"></script>

</head>
<body>
  <div class="search">
    <input type="text" id='jacoco' placeholder="Search for a title..">
  </div> 

  <div class="results"></div>

</body>
</html>

I am trying to do what is suggested in this post but it doesnt work. My js code is:

var typingTimer;                //timer identifier
var doneTypingInterval = 500;  //set wait time until search starts to 5 seconds

//on keyup, start the countdown
$('#jacoco').keyup(function(){
    clearTimeout(typingTimer);
    if ($('#jacoco').val()) {
        alert($("#jacoco").val());
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    }
});

//user is finished typing
function doneTyping () {
    alert('$("#jacoco").val();');
}

So when I am typing in the search box it doesn't print my input.

You are passing a string '$("#jacoco").val();' to alert() . ' should be removed. And also use console.log instead of alert()

 var typingTimer; //timer identifier var doneTypingInterval = 500; //set wait time until search starts to 5 seconds //on keyup, start the countdown $('#jacoco').keyup(function(){ clearTimeout(typingTimer); if ($('#jacoco').val()) { //alert($("#jacoco").val()); typingTimer = setTimeout(doneTyping, doneTypingInterval); } }); //user is finished typing function doneTyping () { console.log($("#jacoco").val()); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!DOCTYPE html> <html> <head> <title>IMDB Search</title> <link rel="stylesheet" href="jacoco.css"> <script src="https://code.jquery.com/jquery-3.4.0.js" integrity="sha256-DYZMCC8HTC+QDr5QNaIcfR7VSPtcISykd+6eSmBW5qo=" crossorigin="anonymous"></script> <script src="jacoco.js"></script> </head> <body> <div class="search"> <input type="text" id='jacoco' placeholder="Search for a title.."> </div> <div class="results"></div> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM