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");
});

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.