The natural / modern solution to this is using placeholder
.
<input placeholder="nome" />
However placeholder is HTML5 and some older browsers do not support. How to do it then? here is an alternative:
If you want to use keydown
, that is when the user starts typing, you can do this by comparing the value attribute with the actual value it currently has:
var input = document.querySelector('input');
input.addEventListener('keydown', function (e) {
var original = this.getAttribute('value');
var novo = this.value;
if (novo == original) this.value = '';
});
jsFiddle: link
The .getAttribute('value');
is always the same as in HTML. What is dynamic and changes according to what is written is the .value
property. So you can know if what's in the input is still the original.