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 9096459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:51:29+00:00 2026-06-16T23:51:29+00:00

Sorry if the question title is a little confusing, but I’m not sure exactly

  • 0

Sorry if the question title is a little confusing, but I’m not sure exactly how to word my problem.

I have the following synchronous ajax call (the purpose of which is to get json file contents).

$.ajax({
    url: "/JsonControl/Events.json",
    dataType: 'json',
    async: false,
    success: function(jsonObj) 
    {
        for (var i = 0; i < jsonObj.events.length; ++i) 
        {
            if(day == jsonObj.events[i].dateNumber && (navDate.getMonth() + monthAdjust) == (jsonObj.events[i].dateMonth -1) && navDate.getFullYear() == jsonObj.events[i].dateYear)
            {
                document.getElementById("cGrid" + gridMod).className="eventDay";
                document.getElementById("cGrid" + gridMod).onmousedown = function(){document.getElementById("eventBox").src="/Event htms/Event.htm"; document.getElementById("eventBox").document.getElementById("title").innerHTML = (jsonObj.events[i].title);}
                document.getElementById("cGrid" + gridMod).style.backgroundColor = "#336633";
                isAnEvent = true;
            }
        }
    }
});

The problem I am having is with the following line (line 12, if ‘$.ajax({‘ is line 1):

document.getElementById("cGrid" + gridMod).onmousedown = function(){document.getElementById("eventBox").src="/Event htms/Event.htm"; document.getElementById("eventBox").document.getElementById("title").innerHTML = (jsonObj.events[i].title);}

I have been told that I need closure, but I can’t make heads or tails of any examples I have seen, as I have never seen syntax set up in such a way, and examples I have tried don’t work (I will give details on what “don’t work” means further down).

This is what I attempted (I replaced the concerning line mentioned above with this one, shown below).

(function(index) {
                document.getElementById("cGrid" + gridMod).onmousedown = function(){document.getElementById("eventBox").src="/Event htms/Event.htm"; document.getElementById("eventBox").document.getElementById("title").innerHTML = jsonObj.events[index].title;}
            })(i);

The error I get is this:

Uncaught TypeError: Cannot call method 'getElementById' of undefined 

I get this error with the original problem line, as well as, with the replacement line, just shown above.

I find this self-executing function stuff pretty new, so I’m not sure how to proceed.

Any advice on how to plot what the actual value of jsonObj.events[i].title at the time it is accessed instead of literally plotting ‘jsonObj.events[i].title’ would be greatly appreciated.

I also tried (guessing, as I was) the valueOf method, but quick research has shown that simply returns the value of a boolean value.

  • 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-06-16T23:51:30+00:00Added an answer on June 16, 2026 at 11:51 pm

    Combination of wrapping the in-loop code (as suggested by Justin) with moving to .contentDocument for <iframe> contents, along with a couple other changes.

    See comments in code or ask below to understand what is happening. (I’m assuming #eventBox is an <iframe>)

    function (jsonObj) {
        for (var i = 0; i < jsonObj.events.length; ++i) (function (i) { // `i` inside function is protected from outside changes
            var cGridMod; // holder to reduce lookups
            if (day == jsonObj.events[i].dateNumber
              && (navDate.getMonth() + monthAdjust) == (jsonObj.events[i].dateMonth -1)
              && navDate.getFullYear() == jsonObj.events[i].dateYear) {
                cGridMod = document.getElementById("cGrid" + gridMod);
                cGridMod.className="eventDay";
                cGridMod.onmousedown = function () {
                    var eventBox = document.getElementById("eventBox"); // holder to reduce lookups
                    eventBox.onload = function () { // set an onload listener so #document exists at time of execution
                        eventBox.onload = null; // unset it so it only fires once
                        eventBox.contentDocument.getElementById("title").innerHTML = (jsonObj.events[i].title);
                          // using `.contentDocument` to get <iframe> document
                    };
                    eventBox.src="/Event htms/Event.htm";
                      // now set location (done after so onload will fire when loaded)
                };
                cGridMod.style.backgroundColor = "#336633";
                isAnEvent = true;
            }
        }(i)); // invoke the function for this iteration of the loop; `i`
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for a long question and not a very descriptive title, but my problem
Not sure the title fully describes the problem/question I'm trying to ask, sorry. One
Sorry for the not very descriptive question title. I'm not very sure how to
First of all Sorry if my question title sounds stupid.... I have the following
Sorry if the title of the question is a little misleading but I was
Not sure if the question is titled clearly, but here goes. I have some
I'm not sure how to work the title of this question, so sorry if
Sorry for question title. I can not find a suitable title. I have UITableView
sorry for the cryptic question title. I'm having a strange problem and i have
Hello everybody and sorry for the rather confusing question-title. I have a Hibernate Class

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.