Anyul Rivas
Anyul Rivas

Reputation: 745

Wait for getJSON response

i'm trying to make a script using Yahoo's YQL in a web applicaion. The script looks over a text paragraph and then it finds some references and appends on the text as a popup. It all goes fine until I have to loop through each reference with class "verso", after getting my response it appends all the references in the last element of the list.

Here is the javascript code (rewrited and simplified)

            var url="";
            var version = "RVR1960";
            var verso = "";

            $("div.post-body").find(".verso").each(function(i){
                $resultado = null;
                verso = $(this).text();
                url = "http://query.yahooapis.com/v1/public/yql?" + "q=select%20*%20from%20html%20where%20url%3D%22"+
                    encodeURIComponent("http://www.biblegateway.com/passage/?search=" + verso +
                    "&version=" + version) + "%22&format=xml'&callback=?";
                $pasaje = $(this);

                $.getJSON(url,function(data){
                    if(data.results[0])
                    {
                        $resultado = null;
                        $resultado = $(data.results[0]).find("div.result-text-style-normal:first");
                        $resultado.find("h5, div, a").remove();
                        $("<div class='cita'><span class='left'>&ldquo;</span>"+
                            $resultado.html()+
                            "<p align='right'><b>"+verso+"</b></p>"+
                            "<span class='right'>&rdquo;</span></div>").appendTo($pasaje);
                    }
                    else
                    {
                        $resultado = $("<p>Pasaje no encontrado.</p>");
                    }
                });
            });

and here is my html:

<div class="post-body"><b class="verso">Juan 3:16</b><b class="verso">Mateo 11:28</b><b class="verso">Juan 1:1</b></div>

I'll appreciate all the help

Upvotes: 1

Views: 2462

Answers (1)

Jaro
Jaro

Reputation: 570

so, I have change several things in your code, now it works, have fun;)

var url="";
var version = "RVR1960";
var verso = "";

$("div.post-body > b.verso").each(function(i){
    resultado = null;
    verso = $(this).text();
    url = "http://query.yahooapis.com/v1/public/yql?"+"q=select%20*%20from%20html%20where%20url%3D%22"+encodeURIComponent("http://www.biblegateway.com/passage/?search="+verso+"&version="+version)+"%22&format=xml'&callback=?";

    $.getJSON(url,function(data){
        if(data.results[0])
        {
            resultado = null;
            resultado = $(data.results[0]).find("div.result-text-style-normal:first");
            resultado.find("h5, div, a").remove();
            $("<div class='cita'><span class='left'>&ldquo;</span>"+resultado.html()+"<p align='right'><b>"+verso+"</b></p>"+"<span class='right'>&rdquo;</span></div>").appendTo($("div.post-body > b.verso")[i]);
        }
        else
        {
            resultado = $("<p>Pasaje no encontrado.</p>");
        }
    });
});

Upvotes: 3

Related Questions