Royal
Royal

Reputation: 752

jquery onclick not happening - Django

js:

$(".collectionlist").click(function() {
        alert('alert')       
        $.ajax({ 
            data:{
                csrfmiddlewaretoken: csrf_token,
                collection:('{{collection}}')                               
            },
            type:'POST',
            url: '/collect/', 
            cache:false,            
            success: function(response) { 
            return true;                        
            }
        });
      });

html:

<form action="." method="POST">{% csrf_token %}
<div id="report-collect">
    <button type="submit" class="collectionlist" >Save-Spreadsheet <img src="{{ STATIC_URL }}images/button-icon-ir-fwd.png" width="12" height="17" alt="" /></button>
</form>
</div>

For button,i had given a class name,written a jquery function to call the file via ajax.The problem is jquery function is not called,i changed the class name and tried.Tried with alert message,alert is not comming.Onclick is not happening.Need help.

Upvotes: 0

Views: 1977

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

It could be because of either one of the following reasons

The collectionlist element is created dynamically, then you need to use event delegation

$(document).on('click', '.collectionlist', function(){
    ....
})

Or your script is not running on dom ready

jQuery(function($){
    $(".collectionlist").click(function() {
        ....
    });
})

Upvotes: 3

Related Questions