• View our reviews on Hot Scripts
  • JS Tutorial

Recent Comments

Powered by Disqus

Back to articles

Pre filled text input

From marketing point of view, visitor is more likely to submit information to form if he or she is any way encouraged to do that. One way to encourage visitor is to prefill input with encouraging text.

Most common mistake to do that is to use predefined text value in input and delete it on focus event. It's a mistake because, for example someone might automatically submit form without invoking focus. Yes it is possible to create onsubmit event etc, but why bother if there is much easier way?

The best practise is to use image and change it on focus and blur, like shown in this script:
    width: 231px;
    height: 33px;
    line-height: 33px;
    border: 0;
    background-color: transparent;
    background-image: url("./pre_input.png");
    background-position: 0 0;
    padding-left: 5px;
    padding-right: 5px;
    font-family: Arial, serif;
    font-size: 15px;
    color: grey;
<p><input type='text' id='example'/></p>
//simple add event function
var add_event = function(element, type, listener){
        element.addEventListener(type, listener, false);
        element.attachEvent('on' +  type, listener);

//get needed input
var elem = document.getElementById("example");

//change to empy image on focus
add_event(elem, "focus", function(){ = "0 33px";

//if value is empty change to text image on blur
add_event(elem, "blur", function(){
    if(elem.value == "")
    { = "0 0";
And here is a working example:

You may also be interested in:

Powered by

blog comments powered by Disqus