Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JQuery, Is it possible to get callback function after setting new css rule?

I have $('.element').css("color","yellow") and I need that next event was only after this one, something looks like $('.element').css("color","yellow",function(){ alert(1); }) I need this because:

$('.element').css("color","yellow");  alert(1); 

events are happen at one time almost, and this moment call the bug in animation effect (alert(1) is just here for example, in real module it's animation)

like image 378
Stasonix Niculin Avatar asked Nov 02 '11 01:11

Stasonix Niculin


People also ask

How does Callback work in jQuery?

To prevent this from happening jQuery provides a callback function for each effect method. A callback function is a function that is executed once the effect is complete. The callback function is passed as an argument to the effect methods and they typically appear as the last argument of the method.

Which function is used in jQuery to change CSS of an HTML?

The css() method in JQuery is used to change the style property of the selected element. The css() in JQuery can be used in different ways. Return value: It will return the value of the property for the selected element.

What is the use of CSS () method in jQuery explain with the help of example?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.

Which jQuery action is used to add the style attribute?

css() as a setter, jQuery modifies the element's style property. For example, $( "#mydiv" ).


2 Answers

you can use promise

$('.element').css("color","yellow").promise().done(function(){     alert( 'color is yellow!' ); }); 

http://codepen.io/onikiienko/pen/wBJyLP

like image 142
user2497228 Avatar answered Sep 18 '22 16:09

user2497228


Callbacks are only necessary for asynchronous functions. The css function will always complete before code execution continues, so a callback is not required. In the code:

$('.element').css('color', 'yellow'); alert(1); 

The color will be changed before the alert is fired. You can confirm this by running:

$('.element').css('color', 'yellow'); alert($('.element').css('color')); 

In other words, if you wanted to use a callback, just execute it after the css function:

$('.element').css('color', 'yellow'); cb(); 
like image 27
Zack Bloom Avatar answered Sep 16 '22 16:09

Zack Bloom