What I was doing is read an XML file and get the data via jQuery into my HTML file.
As I have found in many tutorials I am using jQuery's .get()
method. It serves me well except for one problem!
This is the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<books>
<book title="CSS Mastery" imageurl="images/css.jpg">
<description>
info goes here.
</description>
</book>
<book title="Professional ASP.NET" imageurl="images/asp.jpg">
<description>
info goes here.
</description>
</book>
<book title="Learning jQuery" imageurl="images/lj.jpg">
<description>
info goes here.
</description>
</book>
</books>
Here is my HTML file with jQuery codes:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" >
$("document").ready(function() {
$.get("one.xml", function(d) {
var title = [];
var description = [];
$(d).find("book").each(function() {
title.push($(this).attr("title"));
description.push($(this).find("description").text());
});
console.log(title);
});
});
</script>
</head>
<body>
// BODY contents...
</body>
</html>
What I want to do is I want the titles and descriptions to be returned so that i can use these arrays inside other functions. According to the jQuery code my title
array is being printed in the console
now but when I try to print the title
array outside of the .get()
method it says "title is undefined"
.
I have tried returning the arrays at the end of the function but no luck. I'm not sure if I have made my questions clear or not so I paste the code thats giving me the error below:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$.get("one.xml", function(d){
var title = [];
var description = [];
$(d).find("book").each(function() {
title.push($(this).attr("title"));
description.push($(this).find("description").text());
});
});
console.log(title);
});
</script>
</head>
<body>
// BODY contents...
</body>
</html>
In this block of code it is saying "title isn't defined
and notice I am consoling the title
array outside of the .get()
method.