Run function when clicking on "textarea"

Asked

Viewed 1,105 times

4

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

  • I’m creating a chat, the sending part, reply to the person’s message, it’s all ready, it’s a very big code, then it’s complicated to post here

  • You don’t need to post all the code, just the relevant snippets to your problem. That page has a better description of how to build your example.

1 answer

6

To detect if a text field (textarea) was 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 on jQuery, here it comes:

$('#message').on('click', function() {
    alert('Hello!');
});

Noting that the HTML may be the same.

  • 1

    Please note that this javascript event bind needs to be called on Document.ready, end of the page or on window load.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.