Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .focus() on textarea not working

I got this code:

<textarea id="status" placeholder="Write here..." name="new_entry"></textarea>

and this:

$('#status').focus(function() {
    alert('focused!');
});

I want the alert to start when the textarea is focused on, but it doesn't work...

like image 514
Kevin Lloyd Bernal Avatar asked Mar 27 '13 17:03

Kevin Lloyd Bernal


People also ask

Why is focus () not working?

The reason that's not working is simply because it's not stealing focus from the dev console. If you run the following code in your console and then quickly click in your browser window after, you will see it focus the search box: setTimeout(function() { $('input[name="q"]'). focus() }, 3000);

How to use focus out in jQuery?

jQuery focusout() MethodThe focusout() method attaches a function to run when a focusout event occurs on the element, or any elements inside it. Unlike the blur() method, the focusout() method also triggers if any child elements lose focus. Tip: This method is often used together with the focusin() method.


3 Answers

Here's the working fiddle.

HTML

<textarea id="status" placeholder="Write here..." name="new_entry"></textarea>

JS

$('document').ready(function(){
    $('#status').focus(function() {
        alert('focused!');
    });
});
like image 120
abhshkdz Avatar answered Sep 22 '22 13:09

abhshkdz


If the text area which your are trying to bind focus to is loaded dynamically [using ajax or something], then you have to bind the live event of focus. Something like this

$('#status').live('focus', function() {
alert('focused!');

});

like image 31
Ravi Sankar Rao Avatar answered Sep 22 '22 13:09

Ravi Sankar Rao


Try this:

$(function(){
    $('#status').focus(function() {
        alert('focused!');
    });
});
like image 26
palaѕн Avatar answered Sep 25 '22 13:09

palaѕн