#addEventListener not working

79 messages · Page 1 of 1 (latest)

austere wolf
#

I have the following code:

#
<div class="container">
    <div class="photo-container">
      <div class="photo-section" id="pht1" data-text="" style="cursor: pointer;"> <img src="">
      </div>
    </div>
    <div class="s-container" id="phtdiv">test</div>
  </div>
#
document.addEventListener("DOMContentLoaded", () => {

  console.log("DOM LOADED");

   const photo1 = document.getElementById("pht1");
  photo1.addEventListener("click", () => {
    console.log("clicked");
  }); 
});
#

DOM LOADED is showing in the web console but when I click the div its not logging anything

lament violet
#

The only content inside the div is an empty image. Unless you have CSS specifying size dimensions for this div, it has 0 height and 0 width. That makes it impossible to click on. I don't know what you tried to click to get an output, but it wasn't the div that the event listener is on.

austere wolf
#
.photo-container {
  padding-top: 10px;
  padding-left: 10px;
  display: flex;
  flex-direction: column;
  width: 30%;
}

.photo-section {
  position: relative;
  width: 50%;
  height: 150px;
  margin-bottom: 15px;
  border: 1px solid black;
  overflow: hidden;
}
#

it has width and height declared

lament violet
austere wolf
#

huh

lament violet
#

This works with your exact code. I did not make any changes.

#

But it was impossible without the CSS for the reason I explained above.

austere wolf
#

nah for me it doesnt work on codepen as well

lament violet
lament violet
austere wolf
lament violet
austere wolf
#

Mouse

lament violet
austere wolf
#

DELL

lament violet
austere wolf
#

Box, im not stupid 🙂

lament violet
austere wolf
#

I feel you

#

Don't know what the problem would be

lament violet
austere wolf
#

yep, it works now

lament violet
#

It should not output DOM Content loaded

lament violet
austere wolf
#

When the box is clicked it outputs clicked

lament violet
#

I removed waiting for the domcontent to load.

#

But I am not aware of any reason why that should have been a problem.

austere wolf
#

still in browser mode doesnt work

lament violet
austere wolf
#

I meant that when I open the html page and click on the image, it doesnt log anything in the browser console, I also tried adding some text in the div with the phtdiv id and still doesnt do anything

lament violet
austere wolf
#

I didn't remove it

lament violet
# austere wolf I didn't remove it

I got my version working by removing it. Maybe you thought I meant I removed the message about the content loading. I removed the listener itself but kept the other listener that was inside of it.

austere wolf
#

ok let me try adding defer

#

It's strange because I have more code in the index.js file that works for the other html pages, only for this one it's more complicated

#

same listeners, same methods

lament violet
#

This is the entire revised js that I used:

const photo1 = document.getElementById("pht1");
  photo1.addEventListener("click", () => {
    console.log("clicked");
  }); 

lament violet
austere wolf
#

What do you mean by that

lament violet
austere wolf
#

no

lament violet
austere wolf
#

Yes, still doesn't work

lament violet
austere wolf
#

its 400 lines of code

#

can I DM it?

lament violet
austere wolf
#

50 lines are variables, 250 lines are event listeners

#

:p

lament violet
# austere wolf 50 lines are variables, 250 lines are event listeners

I am not sure if looking at any of that code would help. The codepen only worked after removing the domcontentloaded event listener. I have never seen a problem before that required removing it and it worked fine on my computer with both Firefox and Chrome but it didn't work for you. There is something else wrong besides the JS.

austere wolf
#

well, probably

lament violet
#

chrome://settings/help

austere wolf
#

Does this has anything to deal with it?

lament violet
austere wolf
#

no arguments used in ()

#

did not need any

lament violet
austere wolf
#

line 27

lament violet
# austere wolf

Check your html to see if you have an element with that exact id. Check both spelling and capitalization and double check that it is an id and not a class.

austere wolf
lament violet
# austere wolf

Add a new line between 27 and 28 to output a console log of abisiniana. The goal is to verify that it is not null.

austere wolf
#

wait you gave me an idea

lament violet
# austere wolf

Try the same console log on line 69 to see if it is null right before the attempt to add an event listener.

austere wolf
#

yeah just did that rn, its null

lament violet
austere wolf
#

nope, just verified with ctrl f, but I will change the names anyways

#

Changed the name, still null

lament violet
austere wolf
#

It's fine, thanks for the help