Execute function when clicking on "textarea"

4

How to execute a function when a <textarea> is clicked?

    
asked by anonymous 25.04.2014 / 14:30

1 answer

6

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!');
});

    

25.04.2014 / 15:01