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

  • Home
  • SEARCH
  • 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 3393284
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:01:57+00:00 2026-05-18T04:01:57+00:00

this should be a simple question, but the answer has eluded me for some

  • 0

this should be a simple question, but the answer has eluded me for some time now. I seem to have an error in this code, either a scripting typo, or an error in my logic. Could you kindly clarify my problem?

Here’s the code:

function GetQuestion() {
        $.ajax({
            type: "GET",
            url: "questions.xml",
            dataType: "xml",
            success: function(xml) {
                x = 0;
                x = $(xml).find('Question').length;

                var questionID = $.random(x);

                $(xml).find('Question').each(function(){
                    if(this.ID == questionID) {
                        var text = $(this).find('BODY').text();
                        $('#questionBody')[0].innerHTML = text;
                    }
                }); //close each
            } //close success
        });//close ajax
    }; //close function GetQuestion

It’s meant to read in an XML file, search it for a specific item with a random ID, and plug the contents of BODY into a <p> I have in my HTML file. However, it isn’t working as expected. Where have I made an error?

Thanks, Elliot Bonneville

  • 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-18T04:01:58+00:00Added an answer on May 18, 2026 at 4:01 am

    This is just a general observation and not really an answer, but it may help you in the future:

    //notice that this function is much easier to consume in one glance
    function GetQuestion() {
    
        $.ajax({
            type: "GET",
            url: "questions.xml",
            dataType: "xml",
            success: GetQuestionSuccess
        });//close ajax
    
    }; //close function GetQuestion  // <-superfluous semicolon?
    
    
    
    //notice that this function is much easier to consume in one glance
    function GetQuestionSuccess(xml) {
    
        //let's quit querying each time we need them
        var questions = $(xml).find('Question');  //bonus, it's a jQuery object!
    
        //x = 0; // <-unnecessary assignment. It gets obliterated on the next line.
        x = questions.length; //the count of all "Question"s found
    
        var questionID = $.random(x);
    
        //but wait, read my next comments below
        questions.each(function(){
            if(this.ID == questionID) {
                var text = $(this).find('BODY').text();
                $('#questionBody')[0].innerHTML = text;
            }
        }); //close each
    
        //since you seem to be looking for the index questionID of all the questions, 
        //why not jump to that one instead of looping?
        //also, if you only have one "#questionbody" in your document you can do this more "jquery"ish.
        $('#questionBody')[0].innerHTML = questions[questionID].find('BODY').text();
    
        //only one:
        $('#questionBody').html( questions[questionID].find('BODY').text() );
    
    } //close success
    

    so to clarify :\

    //I shredded it down to a reasonable number of lines. Could be shorter still, albeit less readable.
    //but I think this is pretty readable.
    function GetQuestionSuccess(xml) {
    
        var questions = $(xml).find('Question');
    
        var questionID = $.random(questions.length);
    
        $('#questionBody').html( questions[questionID].find('BODY').text() );
    
    } //close success
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found this example on StackOverflow: var people = new List<Person> { new
I have a new web app that is packaged as a WAR as part
I have a login.jsp page which contains a login form. Once logged in the
This is beyond both making sense and my control. That being said here is
I want to use a temp directory that will be unique to this build.
Let say I have the following desire, to simplify the IConvertible's to allow me
I am using a 3rd-party rotator object, which is providing a smooth, random rotation
(please excuse that I didn't use aliases). I would like my query output to
I'm trying to build a C++ extension for python using swig. I've followed the
I need to develop a file indexing application in python and wanted to know

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.