C.Ronaldo C.Ronaldo - 3 months ago 32
jQuery Question

Callback function on addClass not working

I want the browser to focus on the input after clicking the focus button.

But before that I want it to add a class first and then focus. Is this possible? Or can I only use callback functions on 'animations' like hide, click etc.

I've created a jfiddle: https://jsfiddle.net/6ktgc4h9/5/

$('.input').hide();

$('.focus').click(function()
{
$('.input').show();

$('input').addClass('test', function()
{
$('.search').focus();
});
})

Answer

.addClass doesn't have a callback function.

Callbacks are used for asynchronous operations. They aren't needed for synchronous operations. Just execute the code in the order you want it executed:

$('input').addClass('test');
$('.search').focus();
Comments