3

I am trying to remove the onClick tags in my html and add an EventListener to my external js-file instead, but can't seem to get it to work.

The following lines work:

<input type="text" name="text" id="test">
<input type="submit" name="send" value="Skicka" id="klick" onclick="skriv()">

When removing onclick and adding the following to my javascript, it does not :

document.getElementById("klick").addEventListener("click", skriv);

function skriv() {

    var input = document.getElementById("test").value;
    alert("Hello" + " " + input);

}

Entire html:

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="hello.css">
    <title>
        Multimedia
    </title>
    </head>
    <body>
        <script type="text/javascript" src="hello.js"></script>
        <div>
            <form>
                <p>Skriv ditt namn:</p><br>
                <input type="text" name="text" id="test">
                <input type="submit" name="send" value="Skicka" id="klick">
            </form>
            <p class="lol">
                "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"`enter code here`
            </p>
        </div>
    </body>
</html>
0

4 Answers 4

11

you are missing window.onload, checkout demo with separate script.js file

window.onload = function () {
  var buttonElement = document.getElementById("klick");
  var inputElement = document.getElementById('test');


  if (buttonElement) {
    buttonElement.addEventListener('click', skriv);
  }

  function skriv() {
    var input = inputElement.value;
    alert("Hello " + input);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this works! Thanks Vilas! Good solution.I used it in a php form to call an external js file. The event listeners would not work until I did the above.
2

Place your <script> tag right before your closing </body> tag.
Or use another way to detect your DOM is ready.

<body>


  <div>
    <form>
      <p>Skriv ditt namn:</p><br>
      <input type="text" name="text" id="test">
      <input type="submit" name="send" value="Skicka" id="klick">
    </form>
    <p class="lol">
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"`enter code here`
    </p>
  </div>



  <script> // Or external JS URL
    document.getElementById("klick").addEventListener("click", skriv);

    function skriv() {

      var input = document.getElementById("test").value;
      alert("Hello" + " " + input);

    }
  </script>

</body>

Comments

0

It does not work because when the script is loaded, there are no elements to be found on which it can bind the event.

Solutions.

  • Move your script to the bottom of the body tag.
  • or you can Use Delegate

       document.addEventListener("click", function(e) {
           for (var target=e.target; target && target!=this; target=target.parentNode) {
           // loop parent nodes from the target to the delegation node
               if (target.matches(<selector>)) {
                   handler.call(target, e);
                   break;
               }
           }
       }, false);
    

Comments

0

I would propose just to add the following right before the closing tag:

<script> window.onload=startListen(); </script>

On the first line of your .js-file, you add function startListen(){ * }

You replace the * with all your eventListeners you want.

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.