Veljko Kukic
Veljko Kukic

Reputation: 137

addEventListener not working as it should

I have problem with adding event listener into JS...If i connect button with onclick attribute its working but if I delete it in HTML and try to add eventListener('click', myFunction), its not working. And btw its a simple dropdown button.

In following example in button i deleted onclick="myFunction" and added this - >

JavaScript:

document.getElementById("btn").addEventListener('click', myFunction);

function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
 

HTML:

<button id="btn" class="dropbtn">RECEPT</button>
<div id="myDropdown" class="dropdown-content">
  <ul>
    <li>-5 jaja</li>
    <li>-0.2l mleka</li>
    <li>-100g slanine</li>
    <li>-200g brasna</li>
    <li>-10g putera</li>
    <li>-50g sira</li>
    <p class="recipeInfo">Kuvati na laganoj vatri i ostaviti da se krcka 5 minuta</p>
  </ul>
</div>

I dont know what is the problem nor solution...Any help?

Upvotes: 3

Views: 11995

Answers (5)

Matteo B.
Matteo B.

Reputation: 21

After 6 years, what you can also do, is just add defer in your script tag:

<script type="text/javascript" src="scriptName.js" defer></script>

Upvotes: 0

Luis Bautista
Luis Bautista

Reputation: 31

More than one year later, but still maybe it is useful for someone:

I was struggling with my javascript ajax request that stopped working after server migration to AWS.

After hours trying to figure out what the problem was, I realized that I was using

window.addEventListener("DOMContentLoaded", function(event) { ...}

Instead of

document.addEventListener("DOMContentLoaded", function(event) { ...}

As soon as I changed to "document.", every ajax request to my service got a proper response.

I am not a javascript developer and maybe because of that, it was a nightmare for me

Hope it helps

Upvotes: 3

Franceso Russo
Franceso Russo

Reputation: 53

You have to wait page load. But you don't need jQuery, you can wrap your js code in a self executed function, like this:

(function(){
    // your code
}())

Upvotes: 0

Asad ullah
Asad ullah

Reputation: 716

I think problem is that you are not waiting for page to load completely. Use jQuery to make sure page is loaded.

And then use jQuery on for click event

$(document).ready(function(){

$("#btn").on('click', myFunction);

function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}

});

Upvotes: 2

HugoTeixeira
HugoTeixeira

Reputation: 4884

You are probably trying to access the DOM elements before they are ready. Try placing your Javascript code inside a DOMContentLoaded listener:

document.addEventListener("DOMContentLoaded", function(event) { 
    // put your javascript code here
});

This will guarantee that your Javascript code will have access to all DOM elements. Read more at $(document).ready equivalent without jQuery

Upvotes: 5

Related Questions