How to execute a function when a <textarea>
is clicked?
How to execute a function when a <textarea>
is clicked?
To detect whether a text field ( textarea
) has been clicked with JavaScript pure and unobtrusive , you can do:
HTML:
<textarea id="message"></textarea>
JavaScript:
function $(id) {
return document.getElementById(id);
}
$('message').addEventListener('click', function () {
alert('You clicked on textarea.');
});
To view a demo, click here (jsFiddle) .
Remembering that, for events unobtrusive , a good practice is through addEventListener .
If you want an option in jQuery , here it goes:
$('#message').on('click', function() {
alert('Hello!');
});