How to read the value of a variable which is set inside the jquery slide toggle callback function?
I hope everyone is doing fine.
I am setting a variable inside slide toggle call back function. I need to perform some action based on this variable's value. So, I need to wait for this variable's value to be set in parent method. I think following code snippet will explain better what I am trying to do.
function() {
$(this).next().slideToggle('normal',function(){
if ($(this).is(':hidden'))
{
state = "open";
}
else
{
state = "close";
}
isAnimationCompleted=true;
return true;
});
while(isAnimationCompleted==false)
{
//do nothing
}
isAnimationCompleted=false;
var selectedElement =$.trim(this.textContent)开发者_JS百科;
if(state=="open" )
{
showHelp(selectedElement,state);
}
}
I will sincerely appreciate any help. Thanks.
Use the $.data() method on the object whose state you wish to keep track of. This way, the property-value combination you wish to store is associated with a particular object.
There's a great explanation of it in the documentation: http://api.jquery.com/jQuery.data/.
As far as your code goes, try this:
function() {
// When the page loads, set a default value to the property,
$("#div_you_toggle").data("open",false);
$("#div_you_toggle").slideToggle('normal',function(){
if ($(this).is(':hidden'))
{
$(this).data("open",false);
}
else
{
$(this).data("open",true);
}
isAnimationCompleted=true;
return true;
});
while(isAnimationCompleted==false)
{
//do nothing
}
isAnimationCompleted=false;
var selectedElement =$.trim(this.textContent);
if( $("#div_you_toggle").data("open") == true )
{
showHelp(selectedElement,state);
}
}
There's definitely a bit of optimization that can take place, but this should introduce the basic concept.
精彩评论