Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning XML data parsed via jQuery
    primarykey
    data
    text
    <p>What I was doing is read an XML file and get the data via jQuery into my HTML file.</p> <p>As I have found in many tutorials I am using jQuery's <code>.get()</code> method. It serves me well except for one problem!</p> <p><strong>This is the XML file:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;books&gt; &lt;book title="CSS Mastery" imageurl="images/css.jpg"&gt; &lt;description&gt; info goes here. &lt;/description&gt; &lt;/book&gt; &lt;book title="Professional ASP.NET" imageurl="images/asp.jpg"&gt; &lt;description&gt; info goes here. &lt;/description&gt; &lt;/book&gt; &lt;book title="Learning jQuery" imageurl="images/lj.jpg"&gt; &lt;description&gt; info goes here. &lt;/description&gt; &lt;/book&gt; &lt;/books&gt; </code></pre> <p><strong>Here is my HTML file with jQuery codes:</strong></p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" &gt; $("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); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; // BODY contents... &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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 <code>title</code> array is being printed in the <code>console</code> now but when I try to print the <code>title</code> array outside of the <code>.get()</code> method it says <code>"title is undefined"</code>.</p> <p>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:</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $("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); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; // BODY contents... &lt;/body&gt; &lt;/html&gt; </code></pre> <p>In this block of code it is saying <code>"title isn't defined</code> and notice I am consoling the <code>title</code> array outside of the <code>.get()</code> method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload