-1

I'm trying to use load() to load two divs #follow-x and #follow-y ajaxly with a click of a button.This is what I have tried in success function,but doesn't work,but works if I remove one of the function,so it loads only one div but I want it to load both.Thanks in advance

$('#follow').click(function(){

          $.ajax({

                   type: "POST",
                   url: "{% url 'follow_class' %}",
                   data: {'pk': '{{class.pk}}' , 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                   dataType: "json",
                   success: function(){
                       $('#follow-x').load("{% url 'class_details' %} #follow-x");}
                       function(){
                       $('#follow-y').load("{% url 'class_details' %} #follow-y");}

              }); 
        });
</script>

Dont mind the tags{} I'm using Django

Sr Julien
  • 494
  • 1
  • 8
  • 27
A.Lang
  • 59
  • 1
  • 8
  • Why have two separate functions? Why not just do `$('#follow-x').load(...); $('#follow-y').load(...);`? – Mike Cluck Oct 06 '16 at 15:45
  • the syntax of your code doesn't look correct. mainly, the closing `}` on your first load and the random unnamed and uncalled `function () {` wrapping your second .load. however, surely it would be better to just use $.ajax then extract the two divs from the response rather than sending two and doing the same for each. – Kevin B Oct 06 '16 at 15:50

3 Answers3

1

only the first function is being called on success

put both statements in one function

$('#follow').click(function(){

          $.ajax({

                   type: "POST",
                   url: "{% url 'follow_class' %}",
                   data: {'pk': '{{class.pk}}' , 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                   dataType: "json",
                   success: function(){
                       $('#follow-x').load("{% url 'class_details' %} #follow-x");                        
                       $('#follow-y').load("{% url 'class_details' %} #follow-y");
                    }

              }); 
        });
M B
  • 2,326
  • 24
  • 33
-1

Your second load call is in another function that never gets called

$('#follow').click(function(){

          $.ajax({

                   type: "POST",
                   url: "{% url 'follow_class' %}",
                   data: {'pk': '{{class.pk}}' , 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                   dataType: "json",
                   success: function(){
                       $('#follow-x').load("{% url 'class_details' %} #follow-x");
                       $('#follow-y').load("{% url 'class_details' %} #follow-y");
                   }

              }); 
        });
</script>
aaronofleonard
  • 2,546
  • 17
  • 23
-1

Success is a property of the object passed to AJAX. In your code you attempt to assign it to a function containing the follow-x code, however a syntax error is produced due to the next function statement that violates the obj structure. Try this.

success: function(){
     $('#follow-x').load("{% url 'class_details' %} #follow-x");
     $('#follow-y').load("{% url 'class_details' %} #follow-y");
}

Also consider using the done callback instead of success. More info on that here what is difference between success and .done() method of $.ajax

Community
  • 1
  • 1
nmargaritis
  • 859
  • 7
  • 21