dikei
dikei

Reputation: 439

Ajax call on slideToggle

I am getting some data from the server via Ajax call on button click, but I would like to get the data only when the drop-down goes down, would like to avoid the call when the drop-down goes back up.

Here is my jquery

$('.button').on('click', function(){
$('.test').slideToggle('slow');
 $.ajax({
   type: "GET",
   url: /testUrl.php,
   success: function(result) {
    alert(result);
 });  
});

Thank you

Upvotes: 0

Views: 725

Answers (2)

Abhishekh Gupta
Abhishekh Gupta

Reputation: 6236

You can try this:

$('.button').on('click', function(){
  var $test = $('.test');
  $test.slideToggle('slow');
  if (!$test.is(':visible')) {
    $.ajax({
      type: "GET",
      url: /testUrl.php,
      success: function(result) {
        alert(result);
    });  
  }
});

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167250

You can do this way:

$('.button').on('click', function(){
  if (!$('.test').is(:visible))
    $.ajax({
      type: "GET",
      url: /testUrl.php,
      success: function(result) {
      alert(result);
    });  
  $('.test').slideToggle('slow');
});

Upvotes: 1

Related Questions