How to use jquery to return a text value?
Hello,
I was using ajax on my webpage to process my xml files, however, I wanted to process more than 1 xml file at any given time and started learning jquery. I am a beginner.
I simply do not understand the way the function returning a value. If I use the alert() function just before the return statement (highlighted in red), then the main function calling the subroutine, receives the value returned, else it is a blank value.
A short program I wrote is given below:
--------------------------------
function loadintrocb(pageintroxml, infotag)
{
var onloadintro = "";
/***Get the home page intro and load:***/
$.get(pageintroxml, function(intro){/*Begin jquery instant 5:*/
$(intro).find(infotag).each(function(){/*Begin jquery instant 6:*/
onloadintro += "<div class=\"" + $(this).find('Text1Style').text() + "\">";
onloadintro += $(this).find('Text1').text() + "</div>";
onloadintro += "<div class=\"" + $(this).find('ImgStyle').text() + "\">";
onloadintro += "<" + $(this).find('ImgSrc').text() + "/></div>";
onloadintro += "<div class=\"" + $(this).find('Text2Style').text() + "\">";
onloadintro += $(this).find('Text2').text() + "</div>";
});/*Close jquery instant 6*/
});/*Close jquery instant 5*/
/*Return the html content:*/
return onloadintro;
}
function xmlExecOnLoad(pageintroxml, infotag, bttnlstxml, homebttntag)
{
var loadintro = loadintrocb(pageintroxml, infotag);
$("#menuxpand").html(loadintro);
}
------------------------
I tried the following and quite puzzled with the way the callback function behaves. The function first alerts the value 4, followed by 2, 3 and 1.
function loadintrocb(pageintroxml, infotag)
{
var onloadintro = "";
/***Get the home page intro and load:***/
$.get(pageintroxml, function(intro){/*Begin jquery instant 5:*/
$(intro).find(infotag).each(function(){/*Begin jquery instant 6:*/
onloadintro += "<div class=\"" + $(this).find('Text1Style').text() + "\">";
onloadintro += $(this).find('Text1').text() + "</div>";
onloadintro += "<div class=\"" + $(this).find('ImgStyle').text() + "\">";
onloadintro += "<" + $(this).find('ImgSrc').text() + "/></div>";
onloadintro += "<div class=\"" + $(this).find('Text2Style').text() + "\">";
onloadintro += $(this).find('Text2').text() + "</div>";
alert(2);
});/*Close jquery instant 6*/
alert(3);
});/*Close jquery instant 5*/
alert(4);
/*Return the html content:*/
alert(1);
return onloadintro;
}
If I use the alert twice before the return statement, then I can see the contents stored in the string, which is then returned.
Any help or advice would be much appreciated.
Many thanks
NT