1

I was trying to get my head around jQuery's Ajax. I have a page made up of a number of divs. I also have an XML document generated from a MySql resultset.

In the jQuery function below I am able to populate the titleDiv with data. The question I have is how do I populate the other divs on the page without having to build the page from scratch? I hope this makes sense......

$(document).ready(function() {
    $("#getData").click(function(){
        var data = "";
        $.get("phpAjax.php", function(theXML){
            $('row',theXML).each(function(i){
                var title = $(this).find("Title").text();
                var rating = $(this).find("Rating").text();
                data = data + title; 
            });
            $("#titleDiv").html(data);
            $("#ratingDiv").html(?????);
        });
    });
});

1 Answer 1

1

did u try with?? first decalre variable

 var title='';
 var rating ='';

& then inside each

   title+ = $(this).find("Title").text();
   rating+ = $(this).find("Rating").text();

    $("#titleDiv").html(title);
    $("#ratingDiv").html(rating);
Sign up to request clarification or add additional context in comments.

5 Comments

Sure did...didn't work. I thought that would make the most sense too. I did an alert(rating) and that contained the correct value. But it still hasn't populated the div. I did manage to populate the rating div with data (title) so its not an issue with that either...
do u have multiple divs or only two div title and rating??
nice one, I got it, my variables were local to the function. I had to declare them outside like you said. Cheers!!! And I have loads of divs just shortened it down to 2 for posting sake. thanks again
NEver mind - I see you understood diEcho's point about declaring the variables outside the scope of the function. I thought you said you had done so and it didn't work.
@Tim, thanks Tim, the "sure did...didn't work" was to a post diEcho had that he subsequently edited to include the local variable issue. Thanks again for taking the time.

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.