0

The question is related to this 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.

function parseJSON()
{
    $('#dictionary').empty();
    $.each(data, function(entryIndex, entry) { /*error here*/
        var html = '<div class="entry">';
        html += '<h3 class="term">' + entry['term'] + '</h3>';
        html += '<div class="part">' + entry['part'] + '</div>';
        html += '<div class="definition">';
        html += entry['definition'];
        if (entry['quote']) {
            html += '<div class="quote">';
            $.each(entry['quote'], function(lineIndex, line) {
            html += '<div class="quote-line">' + line + '</div>';
            });
            if (entry['author']) {
                html += '<div class="quote-author">' + entry['author'] + '</div>';
                }
        html += '</div>';
            }
        html += '</div>';
        html += '</div>';
        $('#dictionary').append(html);
        });
}


$(document).ready(function() {
$('#letter-b a').click(function() {
$.getJSON('b.json', parseJSON());
return false;
});
});

This way I keep on getting a "'data' is undefined" error. I'm missing something trivial, I know, but I can't get it...

This is the JSON file (works perfectly with the unnamed function):

[
      {
        "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."
      }
    ]

Thanks in advance for any suggestion.

2
  • pass data as argument to parseJSON...also try changing your function name...I think there is already a in built function with that name Commented Mar 2, 2012 at 13:02
  • There is a parseJSON function in jQuery. What if you rename your function to something else? Commented Mar 2, 2012 at 13:02

2 Answers 2

3

Should be: $.getJSON('b.json', parseJSON);, then in the function declaration, add data as a param I think.

Sign up to request clarification or add additional context in comments.

Comments

1

In this line

$.getJSON('b.json', parseJSON());

You're passing in a call to the function parseJSON as the callback.

Most likely you wanted to pass the function itself, like so:

$.getJSON('b.json', parseJSON);

In the code you have now, parseJSON() is first called and its result is passed in as callback.

Also, you'll need to make sure the signature of the callback function complies with what jQuery expects - see: http://api.jquery.com/jQuery.getJSON/ So in your parseJSON function, you would probably need to add data as an argument, like so:

function parseJSON(data) {
   ...your code here...
}

(Note that it doesn't always have to be wrong to use a function call as callback. In case your function passes the actual callback function back as return value, then you could have a call there. For example:

function getParseCallBack() {
    return parseJSON;
}

Then you could do:

$.getJSON('b.json', getParseCallBack());

I'm not suggesting that you use this at this point - I'm just trying to clarify the difference between a function and a call to a function.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.