Reputation: 13006
Page gets an AJAX result every now and then. Result is just a random boolean (true/false).
Based on the result certain element on the page must be shown or hidden. Notice that I cannot just toggle()
it as I need to know whether it's true or false first.
So:
var result = ajax();
if (result) {
$('#myElement').show();
}
else {
$('#myElement').hide();
}
There is also a much shorter way to put this:
$('#myElement').toggle(ajax());
The toggle() method takes a boolean parameter called "showOrHide" to determine whether to show or hide the element.
Same does the toggleClass() method. Except for some reason that parameter is called "switch" instead.
The question: how do I apply that parameter to slideToggle() or fadeToggle()? $('#myElement').slideToggle(ajax())
didn't work.
Or the only way I can use it is:
ajax() ? $('#myElement').slideDown() : $('#myElement').slideUp()
Upvotes: 0
Views: 67
Reputation: 6230
try this
element.toggle(1000, ajax() ? 'slideDown' : 'slideUp', function() {
console.log("Complete!");
});
Upvotes: 1