Kevin Lloyd Bernal
Kevin Lloyd Bernal

Reputation: 363

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...

Upvotes: 10

Views: 28026

Answers (5)

Jamie de Fretesz
Jamie de Fretesz

Reputation: 1

try this instead :

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

hope it'll help

Upvotes: -1

Ravi Sankar Rao
Ravi Sankar Rao

Reputation: 1070

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

});

Upvotes: 2

Bizmarck
Bizmarck

Reputation: 2698

Your jquery is written correctly. See this similar fiddle. Check with a debugger (in Chrome or Firebug), is the focus method being called? You can try putting the code in a $('document').ready() function.

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

$('#status').focus(function() {
    $('#status2').val("wheee");
});

Upvotes: 0

palaѕн
palaѕн

Reputation: 73966

Try this:

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

Upvotes: 1

abhshkdz
abhshkdz

Reputation: 6365

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

Upvotes: 3

Related Questions