0

From researching I have found that when you load a page via ajax it only loads plain code (not jquery or javascript).

I then found a solution on this site after working out .live is depreciated.

My problem is that I can't find an event for '.on' that will load the jquery as soon as the ajax has been called (keeping in mind I am running the .on event on my index.php page).

In this fiddle, how could I get the background css changes to work on load rather than on click in the circustance that the html is being loaded via ajax?

$("#example").on('click', '.divexample', function() {
    $(this).css("background-color","red");
});
Community
  • 1
  • 1
Bjaeg
  • 308
  • 1
  • 9

1 Answers1

1

JQuery .ajax() call is super-javascript-friendly. It should work with minimum overhead. Please make sure that your server response is returning:

Content-Type:text/javascript; charset=utf-8

If this is the case, the code should come back and run immediately.

Unless you have cross-site origin issues going on...

So - once the response content type is correct, you'd just want the response to come back with:

$("#example .divexample").css("background-color","red");
Darren Hicks
  • 4,610
  • 1
  • 29
  • 32
  • I was actually loading the ajax in via javascript originally, I didn't even know .ajax() was a thing.. I have switched to this method and it is super easy to work with! I simply added the .css changes to the request. Thank you @DarrenHicks – Bjaeg Nov 29 '15 at 13:34