Home  >  Article  >  Web Front-end  >  Execute script when user writes something in HTML search field?

Execute script when user writes something in HTML search field?

王林
王林forward
2023-08-28 13:21:071000browse

当用户在 HTML 搜索字段中写入内容时执行脚本?

Use the onsearch attribute in HTML to execute the script when the user types in the search box and presses the ENTER or x key.

Example

You can try running the following code to implement the onsearch property −

<!DOCTYPE html>
<html>
   <body>
      <p>Search something and press ENTER.</p>
      <input type = "search" id="inputID" onsearch = "display()">
      <p><strong>Note:</strong> It won&#39; work in Internet Explorer and Firefox.</p>
      <p id="test"></p>
      <script>
         function display() {
         var a = document.getElementById("inputID");
         document.getElementById("test").innerHTML = "Searched for - " + a.value;
         }
      </script>
   </body>
</html>

The above is the detailed content of Execute script when user writes something in HTML search field?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete