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

Execute script when user writes something in HTML search field?

王林
Release: 2023-08-28 13:21:07
forward
1091 people have browsed it

当用户在 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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template