I have a webpage with a link to make an AJAX request. When the following .js comes back the toggleClass
function does not work.
$(document).ready(function(){
$("td").click(function(){
$(this).toggleClass("br");
});
});
If I replace toggleClass("br")
with something like addClass("br")
then that does work.
Furthermore, toggleClass
works fine if I put the .js into the html page or if I run it from the console. It seems that something about both toggleClass
and AJAX requests together stops this code from working but I have no Earthly idea as to why that might be.
UPDATE
I've figured out the problem. I had accidentally included jQuery two times and so javascript from AJAX requests was being run twice. Hence why only toggleClass
was "not working" while addClass
and removeClass
were.
The only mystery left is why this was only the case when the .js came from an AJAX request as opposed to when it was in the HTML itself.