tags:

views:

45

answers:

2

Right now you click on a link, it send ajax call and on success it prepends the output in . Not i would like to, if you click on the link while its open then it should close/hide the output.

function MoreFriendOptions(friend){
$.ajax({ 
       type: "POST",
       url: "misc/GetMoreFriend.php",
    data: {
    mode: 'ajax',
    friend : friend
    },
       success: function(msg){
      $('#MoreFriendInfo'+friend).prepend(msg);

        }
     });
}

echo "<div id='MoreFriendInfo".$showInfo['bID']."'></div>";
   <div style="float: right;">
  <a href="javascript:void(0);" onclick="MoreFriendOptions(<?=$showInfo["bID"];?>)">mer</a>
   </div>
+1  A: 

you need the toggle method.

$('a.mylink').toggle(function() {
  //do your ajax stuff here
}, function() {
  //do your close and hide here
});
Moin Zaman
What if the user clicks many times? It'll repeatedly fire off an AJAX request every other time :) You'd need to disable the link and re-enable it on `complete` here.
Nick Craver
Rather than doing the ajax request each time, perhaps add in a suggestion about checking to see if the content already exists but is hidden.
rchern
Good point Nick! But I think it depends on the scenario, if every 'show' needs to be a fresh server request then its ok. otherwise yes, before doing the ajax stuff, do a check to see if there is content in the element populated by ajax and its open (by a class or :visible), if not, only then do the ajax stuff
Moin Zaman
@Moin - A check in the method doesn't take care of the issue...clicking while the ajax request is being made :)
Nick Craver
So should this do it? It should be so that when you close it then if you press again it should run a ajax call again..
Karem
@Nick: The thick plottens... I can think of ajaxStart, ajaxStop and ajaxComplete.. you could disable the link, once clicked and then enable it back again on ajaxSuccess or ajaxComplete but yes, but i think in most scenarios if the amount of data and response times are ok, it should be fine.
Moin Zaman
@Karem: yes the code above, if you want to do a fresh ajax call each time should work. with the exception of firing an ajax call again if the link is clicked quickly, unless you handle it in some way as suggested above.
Moin Zaman
A: 

If you have a setup something like

<div class=friends">
    <div class="friend1">John Doe</div>
    <div class="friend2">Jane Doe</div>
</div>

then if you save the index for positioning, you could remove/hide elements you've added.

rchern