Reputation: 103
When I load the page, document.on('click',...)
requests get sent instantaneously regardless of the condition
Example:
$(document).ready(function(){
var poll = new Poll();
if($('.loadPoll')[0]){ //this works fine
poll.loadPoll(function(){
console.log("loaded poll successful");
});
}
$(document).on('click', '#SubmitVote', poll.afterSubmit(function(){
console.log("Poll has been voted on!");
})); //this gets sent regardless of clicks
});
Even if there is no element with SubmitVote
as an ID it gets sent automatically
EDIT:
changed to:
$(document).ready(function(){
var poll = new Poll();
if($('.loadPoll')[0]){
poll.loadPoll(function(){
console.log("loaded poll successful");
});
}
$(document).on('click', 'button.SubmitVote', function(){
poll.afterSubmit(function(){
console.log("Poll has been voted on!");
});
});
}
Upvotes: 0
Views: 105
Reputation: 19549
Felix Kling correctly identified the issue in his/her comment, but I'll expand a bit further - your code is executing a function (poll.afterSubmit
) and assigning the result of that function call as the click handler. Unless that function returns a function, this probably isn't what you want.
$(document).on('click', '#SubmitVote', function(){
console.log("Poll has been voted on!");
});
The above strips out the poll.afterSubmit()
call and simply assigns the handler function. This may or may not be sufficient to meet your needs, it's not clear from your question exactly what you're attepting with poll.afterSubmit
.
Upvotes: 2