Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:12:10+00:00 2026-05-31T09:12:10+00:00

I am trying to get my $.getJSON to work but it returns nothing. Here

  • 0

I am trying to get my $.getJSON to work but it returns nothing. Here is my jQuery:

$('#courseSelect').bind('change', function()
{
    data = $.getJSON('lessons.js');
    var html ='<select name="lessonSelect id="lessonSelect">';
    var len = data.length;
    alert(data.length); //this alerts "undefined"
    for (var i = 0; i< len; i++) {
        html += '<option value="' + data[i].value + '">' + data[i].name + '</option>';
    }
    html +='</select>';
    alert(html); //this alerts <select...></select> 
    //but no option tags because there's nothing to loop through
    $('lessonsDiv').html(html);
});

This is my lessons.js file

   {"lessons": 
     [
          {"value": "1", "name": "Lesson 1"},
          {"value": "2", "name": "Lesson 2"},
          {"value": "3", "name": "Three"}
     ]
    }

Ultimately, what I’m trying to do is create a new select list of lessons when a course first is selected. But for now, I’m just trying to get this getJSON method to work but it’s not. Be great if anyone can see what’s wrong with. Is it expecting a different sort of data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:12:12+00:00Added an answer on May 31, 2026 at 9:12 am

    The call to getJSON() is asynchonrous; the result is not available immediately. What is returned from getJSON() is the jqXHR object which is monitoring the state of the asynchoronous request.

    You need to register a callback (which is what the second parameter is for), which is executed after the response is available. The jqXHR object which is handling the asynchronous request will execute your callback automatically once the request has completed successfully.

    $('#courseSelect').bind('change', function() {
        $.getJSON('lessons.js', function(data) {
            var html = '<select name="lessonSelect id="lessonSelect">';
            var len = data.length;
            alert(data.length); //this alerts "undefined"
            for (var i = 0; i < len; i++) {
                html += '<option value="' + data[i].value + '">' + data[i].name + '</option>';
            }
            html += '</select>';
            alert(html); //this alerts <select...></select> 
            //but no option tags because there's nothing to loop through
            $('lessonsDiv').html(html);
        });
    });​
    

    Once you’ve done this, data would be an object such as;

    var data = {"lessons": 
     [
          {"value": "1", "name": "Lesson 1"},
          {"value": "2", "name": "Lesson 2"},
          {"value": "3", "name": "Three"}
     ]
    };
    

    So you’d actually need to use data.lessons to access the array you’ve got (and data.lessons.length to retrieve the length of it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use jQuery.getJSON to get data from a domain I don't own
I'm trying to get a hold of the data returned from getJSON, but I
I am trying to use the getJSON function in jQuery to import some data
I am trying to send a variable with jQuery's getJSON function but it doesn't
I'm trying to get Jquery to read a json file, But unfortunately I can
I'm trying to get this Jquery.parseJson to work without success. <input type=text id=query /><button>search</button><br
I'm trying to make a simple function with js & jquery but I can't
Trying to get an ASP application deployed; it worked for a while but then
Trying to get comfortable with jQuery and I have encountered some sample code that
Trying to get this to work, with no luck: [DataMember] public Type ParameterType {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.