0

I have a simple Javascript function to be called when a button on the page is clicked. But it is getting called as soon as the page loads. Can anyone please tell me what the problem here is?

The HTML is:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn">ClicMe!</button>
    <div id="demo"></div>
    <script src="script.js"></script>
  </body>
</html>

While the 'script.js' file is as follows.

var url = "example.txt";

function loadDoc(url, cFunction) {
  var xhttp;
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
      cFunction(this);
    }
  };
  xhttp.open("GET", url, true);
  xhttp.send();
}

function myFunction(xhttp) {
  document.getElementById("demo").innerHTML = xhttp.responseText;
}

var btn = document.getElementById("btn");
btn.onclick = loadDoc(url, myFunction);
1
  • you are calling the function here btn.onclick = loadDoc(url, myFunction); Commented Sep 26, 2020 at 11:35

6 Answers 6

2

You must attach an event listener that invoke the function when a user clicks a button:

btn.addEventListener('click', () => {
  loadDoc(url);
});
Sign up to request clarification or add additional context in comments.

Comments

1

The last line of your script is calling the function instead of assigning the handler to it. Since you have arguments that you want to call it with, you need to use something to bundle the arguments and function together for when it is called. If you replace the last line with the following, it should work:

btn.onclick = loadDoc.bind(undefined, url, myFunction) // "this" value is not needed, so it is left undefined

1 Comment

Thanks. Your solution is very compact and it worked. I had earlier just found out the problem but I had solved it by calling loadDoc with parameters from another function and then assigning that function to onclick, but yours is a better solution.
0

use an arrow function like this:

btn.onclick = () => loadDoc(url, myFunction);

1 Comment

Thanks for the reply. I had used the same method as yours but not compacted it in an arrow function, rather a separate function. Your is more compact, in a single line.
0

You are explicitly calling loadDoc() function in your JS file. You should try this -

var url = "https://jsonplaceholder.typicode.com/posts";

function loadDoc() {
  var xhttp;
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
    document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", url, true);
  xhttp.send();
  
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn" onclick='loadDoc()'>ClicMe!</button>
    <div id="demo"></div>
    <script src="script.js"></script>
  </body>
</html>

Comments

0

var url = "https://jsonplaceholder.typicode.com/posts";

function loadDoc() {
  var xhttp;
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
    document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", url, true);
  xhttp.send();
  
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn" onclick='loadDoc()'>ClicMe!</button>
    <div id="demo"></div>
    <script src="script.js"></script>
  </body>
</html>

Comments

0
btn.onclick = loadDoc(url, myFunction);

This is assigning the return value of loadDoc to btn.onclick not the reference of it. In order to get the return value of a function you have to run the function. So what you want to do is assign the function reference, but you have params you are trying to pass.

btn.addEventListener("click", () => loadDoc(url, myFunction));

Using addEventListeren is best practice. This assigns the reference of an anonymous function that calls loadDoc with the params you want to pass.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.