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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:56:47+00:00 2026-06-02T13:56:47+00:00

I created a some jquery code which gets some json data and displays it

  • 0

I created a some jquery code which gets some json data and displays it on the page. In order to reuse it, i wrapped it in a function:

$(function(e) {
var answer;
id = 1;

var question = function() {
    $.getJSON('getjson.php?id=' + id, function(data){
        console.log(data);
    var $htmlString = "";
     $.each(data, function(index) {
        $htmlString += "<h2>" + data[index].question + "</h2>";
                str =  data[index].answer;
                 answer = data[index].answer.split(', ');

    $htmlString += "<ul>";
    for(var index in answer) {

       $htmlString += "<li>" + index + " : " + answer[Math.floor(Math.random() * answer.length)] + "</li>";

    }
    $htmlString += "</ul>";
    });$('#content').html($htmlString);
    });
};
});

Now, the first time the page loads, I need that function to run by itself, then when I click on ‘next’ the id increases and the function is triggered.
The above function works great when I use the click trigger, but how can I trigger it right after the document is ready.

Thanks

  • 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-02T13:56:48+00:00Added an answer on June 2, 2026 at 1:56 pm

    you can also use:

    $(function(){
      (question = function(){
        // your code
      })();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code which is using delegate method of jquery. e.g.: $(document).delegate('.forImg','dblclick',function(event){. .
Some background: I have a site header and created some jQuery code that displays
In html page some tags are dynamically created using jquery and contents are loaded
I've a JavaScript function in my page through which i make some elements in
I'm using the contents() function in jQuery in order to obtain some text within
I created a some javascript/jquery code to filter an html table. The filter works
Some jQuery function in my code is inserting the string: jQuery15206649508478338135_1314906667378 into user-provided feedback.
I've created some code which people can copy and paste into their websites, and
I created some widgets with jQuery UI and I notice that buttons have a
I created a somefile.js where somefile.js contains some jQuery method. So for instance it

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.