Note that there are some explanatory texts on larger screens.

plurals
  1. POFetching json data with jQuery - named function vs. anonymous
    primarykey
    data
    text
    <p>The question is related to <a href="https://stackoverflow.com/questions/4019950/how-to-read-json-arrays/9524431">this</a> one, but instead of using an anonymous function like the linked example, I'd prefer using a named function in order to reuse it as much as I need.</p> <pre><code>function parseJSON() { $('#dictionary').empty(); $.each(data, function(entryIndex, entry) { /*error here*/ var html = '&lt;div class="entry"&gt;'; html += '&lt;h3 class="term"&gt;' + entry['term'] + '&lt;/h3&gt;'; html += '&lt;div class="part"&gt;' + entry['part'] + '&lt;/div&gt;'; html += '&lt;div class="definition"&gt;'; html += entry['definition']; if (entry['quote']) { html += '&lt;div class="quote"&gt;'; $.each(entry['quote'], function(lineIndex, line) { html += '&lt;div class="quote-line"&gt;' + line + '&lt;/div&gt;'; }); if (entry['author']) { html += '&lt;div class="quote-author"&gt;' + entry['author'] + '&lt;/div&gt;'; } html += '&lt;/div&gt;'; } html += '&lt;/div&gt;'; html += '&lt;/div&gt;'; $('#dictionary').append(html); }); } $(document).ready(function() { $('#letter-b a').click(function() { $.getJSON('b.json', parseJSON()); return false; }); }); </code></pre> <p>This way I keep on getting a "<em>'data' is undefined</em>" error. I'm missing something trivial, I know, but I can't get it...</p> <p>This is the JSON file (works perfectly with the unnamed function):</p> <pre><code>[ { "term": "BACKBITE", "part": "v.t.", "definition": "To speak of a man as you find him when he can't find you." }, { "term": "BEARD", "part": "n.", "definition": "The hair that is commonly cut off by those who justly execrate the absurd Chinese custom of shaving the head." }, { "term": "BEGGAR", "part": "n.", "definition": "One who has relied on the assistance of his friends." }, { "term": "BELLADONNA", "part": "n.", "definition": "In Italian a beautiful lady; in English a deadly poison. A striking example of the essential identity of the two tongues." } ] </code></pre> <p>Thanks in advance for any suggestion.</p>
    singulars
    1. This table or related slice is empty.
    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