The onfocus
and onblur
events work on all form elements and anchors, you can try just making your input
a textarea
, and it will work, but I would encourage you to do your event binding programmatically.
Something like this:
var textarea = document.getElementById('textareaId'),
message = 'Click here to type';
textarea.value = message; // set default value
textarea.onfocus = textarea.onblur = function () {
if (this.value == '') {
this.value = message;
} else if (this.value == message) {
this.value = '';
}
};
Try the above example here.
jQuery version:
$(function () {
var message = 'Click here to type';
$('#textareaId').val(message); // set default value
$('#textareaId').bind('focus blur', function () {
var $el = $(this);
if ($el.val() == '') {
$el.val(message);
} else if ($el.val() == message) {
$el.val('');
}
});
});