SoFunction
Updated on 2025-03-03

Example of text box placeholder prompt text function implemented by JavaScript

This article describes the text box placeholder prompt text function implemented by JavaScript. Share it for your reference, as follows:

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> JSText boxplaceholderhint</title>
</head>
<body>
  <input  type="text" value="Please enter keywords">
</body>
<script>
     = function() {
      var defaultValue = "Please enter keywords";
      var input = ("input");
       = "grey";
       = function() {
        if ( == defaultValue) {
          ="";
          setCursorPosition(this, 0);
        }
      };
       = function() {
        if ( == "") {
           = defaultValue;
        }
      };
       = function(e) {
        e = e || ;
        var key =  ||  || ;
        if ( == defaultValue) {
           = "";
           = "black";
        }
        if ( == 1 && key == 8) {
           = defaultValue;
           = "grey";
          setCursorPosition(this, 0);
        }
      };
    };
    function setCursorPosition(elem, index) {
      if () {
        ();
        (index, index);
      }
      else if () {
        var range = ();
        (true);
        ('character', index);
        ('character', index);
        ();
      }
    }
</script>
</html>

Interested friends can use itOnline HTML/CSS/JavaScript code running toolhttp://tools./code/HtmlJsRunTest the running effect.

For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript page element operation skills》、《Summary of JavaScript DOM skills》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript animation special effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.