Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Refresh div after another jquery action?

Tags:

jquery

refresh

How do I refresh element (div, class whatever) after another jquery action?

I have this action which is deleting a record from database and I need to refresh div which is fetching some other data from database as well...

Well here's code:

$(function() {
    $(".delete").click(function() {
        var commentContainer = $(this).parent();
        var id = $(this).attr("id");
        var string = 'id='+ id ;

        $.ajax({
           type: "POST",
           url: "delete.php",
           data: string,
           cache: false,
           success: function(){
               commentContainer.slideUp('slow', function() {$(this).remove();});
           }
         });

        return false;
    });
});

Thanks for all help! :)

like image 696
Marko Avatar asked Sep 28 '09 16:09

Marko


People also ask

How do I reload a DIV without reloading the whole page?

Use this. $('#mydiv'). load(document. URL + ' #mydiv');

How do you refresh modal after Ajax success?

Just reload the header.

What is. after in jQuery?

The after() is an inbuilt function in jQuery which is used to insert content, specified by the parameter for the each selected element in the set of matched element. Syntax: $(selector). after(A);


2 Answers

During an AJAX call you can use the success function to run commands after a successful call. You already have a function in your code so its a simple matter of updating that function. The second div that you will like to update can be loaded with AJAX too.

 $.ajax({
  //other options
  success:function(){
       commentContainer.slideUp('slow', function() {$(this).remove();});
       $('#otherdiv').load('urlofpagewith info.php');
  }

 });
like image 133
Vincent Ramdhanie Avatar answered Sep 28 '22 04:09

Vincent Ramdhanie


Simply expose the data parameter of the success callback and replace the contents of whichever element, in this example div id="someDiv":

$.ajax({
   type: "POST",
   url: "delete.php",
   data: string,
   cache: false,
   success: function(data){
    commentContainer.slideUp('slow', function() {$(this).remove();});
    $('#someDiv').html(data);
  }
 });

See ajax options for more information.

Also, for the love of god don't use type names as variable names, it's really scary and terrible practice, even though string is not a reserved word in Javascript (use data: str or data: dataString instead).

like image 37
karim79 Avatar answered Sep 28 '22 06:09

karim79