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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:41:16+00:00 2026-06-13T09:41:16+00:00

I made a very simple Ajax test code snippet: <!DOCTYPE html> <html> <head> <script

  • 0

I made a very simple Ajax test code snippet:

<!DOCTYPE html>
<html>
    <head>
        <script src='main.js'></script>
    </head>
    <body>
        <button id='get-content'>Click</button>
        <p></p>
    </body>
</html>

main.js

(function(){
    window.addEventListener('load', function(){
        var xml_request = new XMLHttpRequest();
        xml_request.open('GET', '/test.txt', true);
        xml_request.addEventListener('readystatechange', change_content);
        var button_element = document.getElementById('get-content'); 
        button_element.addEventListener('click', fireup(xml_request));
        function fireup(xml_request) {
            xml_request.send();
        }
        function change_content(){
            if (xml_request.readyState == 4 && xml_request.status == 200) {
                document.getElementsByTagName('p')[0].textContent = xml_request.responseText;
            };  
        };
    });
})();

it works, but except, the ajax request send and change the p tag, without I hit the button,
any idea?

  • 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-13T09:41:17+00:00Added an answer on June 13, 2026 at 9:41 am
    button_element.addEventListener('click', fireup(xml_request));
    

    In this line, you call fireup(xml_request), and assign whatever it returns (nothing) as a listener for the click event. You probably want something like this:

    button_element.addEventListener('click', fireup);
    function fireup() { ...
    

    Since xml_request is in scope for the fireup function, you don’t need to pass it as a parameter.

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

Sidebar

Related Questions

I made this very simple code for button clicks. Clicking one button changes it's
Very simple question, I made the following program : #include <stdlib.h> int main(int argc,
I made a very simple rails app on local host and deleted public/index.html and
I made a very simple button click event handler , I would like to
I just made a very simple test app using documentation from MSDN. All I
I made a very simple application to store variables that can be edited onto
So I made this (very simple) program with a swing GUI with NetBeans, and
I recently made a very simple practice program in Python, that takes user input
I've made a very simple aspect, and found a problem when debugging it (see
I've made a very simple REST controller method with Spring 3.0.0.RC1 that uses hibernate

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.