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...
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);
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.
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!');
});
});
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!');
});
Try this:
$(function(){
$('#status').focus(function() {
alert('focused!');
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With