0

I have function in JS:

setRating();

And Jquery ready body with calling this function:

$(function(){
   setRating($('.ratings'));
});

Problem is that when I get HTML content from AJAX on page with elements .ratings then previous code does not work.

Seem I need to call setRating() in AJAX response after getting content?!

3
  • More relavant code please Commented Jun 19, 2015 at 9:30
  • element returned from ajax? How it will be in response? can you elaborate bit? Commented Jun 19, 2015 at 9:35
  • Please show us your ajax code. Commented Jun 19, 2015 at 9:42

2 Answers 2

1

Your issue is that this

$(function(){
   setRating($('.ratings'));
});

Is just shorthand for $(document).ready(function() { .. });

That code is only ever executed once, when the document is marked as "ready". You will indeed need to call setRating() when your new content arrives over AJAX

Sign up to request clarification or add additional context in comments.

2 Comments

When I tried to call setRating() in success response AJAX, i get error: setRating is not defined. Because function is inside $(function(){});. How to resolve it?
Thats a scoping issue, you need to do $(function() {var setRating = function() { ... }; $.ajax(url { .. });}). setRating just needs to be defined in the same scope or a parent scope of the ajax call
0

you have to call this function after ajax is done somethig like this:

 request = $.ajax({
                        ...
                    });
 request.done(function() {
                          ...
     setRating($('.ratings'));
 })

because when is function outside of request.done its called before content is created

1 Comment

I tried, it gives me undefined function, because setRating is inside document ready Jquery

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.