0

I am trying to create a button that will add text when clicked. It works when the javascript is in the html file but does not when I link it externally.

HTML:

<input id="ex" type="text">
<button onclick="createList()">Create List</button>
<p id="demo">Click button</p>
<script src="app.js"></script>

Javascript:

    document.addEventListener("DOMContentLoaded", () => {
      function createList() {
        var x = document.getElementById("ex").value;
        document.getElementById("demo").innerHTML = x;
      }
    })
3
  • What do you mean with link it externally? Commented May 23, 2020 at 20:30
  • Do you have any errors in the console? Commented May 23, 2020 at 20:34
  • 2
    There is no need to nest your creatList() function in a DOMContentLoaded event handler. Just add your <script> reference to the HTML page, right before the closing body tag and the script need only contain your createList() function. Also, the way your reference is set up, app.js must be in the same directory as the HTML page. Commented May 23, 2020 at 20:38

3 Answers 3

2

By declaring your createList() function inside the event callback, you are actually scoping this function to the callback itself. You can just declare it as a function in your base javascript and it should be all good.

Before :

document.addEventListener("DOMContentLoaded", () => {
function createList() {
    var x = document.getElementById("ex").value;
    document.getElementById("demo").innerHTML = x;
}

After :

function createList() {
  var x = document.getElementById('ex').value;
  document.getElementById('demo').innerHTML = x;
}

If you want to wait for the DOM to be loaded before you declare your function, then you should attach your listener inside of the event callback like this :

document.addEventListener("DOMContentLoaded", () => {
  document.getElementById('myButton').onclick = function() {
    var x = document.getElementById('ex').value;
    document.getElementById('demo').innerHTML = x;
  }
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this

<input id="ex" type="text">
<button onclick="createList()">Create List</button>
<p id="demo">Click button</p>
<script src="app.js"></script>

APP.JS

  function createList() {
    var x = document.getElementById("ex").value;
    document.getElementById("demo").innerHTML = x;
  }

Comments

1

createList is inside another function (arrow function of addEventListener), so it is not accessible in your html.

put it out of addEventListener

or another way - do onclick attachment inside addEventListener, not in html, like:

document.addEventListener("DOMContentLoaded", () => {
  document.getElementById('myButton').onclick = function() {
    var x = document.getElementById("ex").value;
    document.getElementById("demo").innerHTML = x;
  }
});

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.