JavaScript implements the default method of displaying the background image in a text box after it has gained focus

  • 2020-06-22 23:54:33
  • OfStack

This article illustrates how JavaScript implements the default display of background images in a text box that disappears when focus is gained. Share to everybody for everybody reference. The details are as follows:

html code:


<form name="searchform" id="search-form">
  <div>
    <b>Search</b>
    <input type="text" name="txtInput" 
    title="Enter the terms you wish to search for." />
    <input type="submit" value="GO!" class="submit"
    style="cursor: pointer;" />
  </div>
</form>

JS code:


<script type="text/javascript" language="javascript">
 (function() {
  var id = document.getElementById('search-form');
  if (id && id.txtInput) {
   var name = id.txtInput;
   var unclicked = function() {
     if (name.value == '') {
       name.style.background = '#FFFFFF url(images/googbg.png) left no-repeat';
     }
    };
    var clicked = function() {
     name.style.background = '#ffffff';
    };
  name.onfocus = clicked;
  name.onblur = unclicked;
  unclicked();
  }
 })();
</script>

Hopefully, this article has helped you with your javascript programming.


Related articles: