1

I have been working on a project and have iterations of it (ensuring that each worked with its intended functionality before moving on).

As I went to start working today, a key part of my functionality has stopped working; even worse, opening up previous iterations (which have NOT been modified) also do not work for this feature.

I have not edited the code whatsoever.

The relevant code is here:

window.onload = function(){ // test for clicks on list
  var ul = document.getElementById("list");
  ul.onclick = function(event){
    var target = getEventTarget(event);
    //do stuff
  }
}

and the function getEventTarget

function getEventTarget(e){
e = e || window.event;
return e.target || e.srcElement;
}

It is not doing anything with the click event (not producing any console logs). As I reiterate, it was working perfectly fine prior to now.

I have restarted my computer, completely cleared the browsers history (Chrome), tried the code on another computer (which it also was previously working on, to no avail), and tried different browsers.

What do I do?

EDIT:

HTML:

<div class="mylist hidden">
<div class="container">

<div id="addplaylist"><a><span class="glyphicon glyphicon-adjust"></span>
  <span>Here's my List</span>
</a></div>
<br />

<ul id="list">
</ul>

</div>
</div>
12
  • has your HTML changed?
    – Jhecht
    Feb 12, 2017 at 3:51
  • 1
    I mean this with all sincerity, but have you checked your Internet connection?
    – Rich
    Feb 12, 2017 at 3:56
  • 1
    My HTML has not changed whatsoever. And yes, MadGab, my internet connection is working fine.
    – helpisgood
    Feb 12, 2017 at 3:57
  • please post up your HTML just to check, because I just tested your code in the snippet editor with a list with the correct ID and it worked just fine for me.
    – Jhecht
    Feb 12, 2017 at 3:58
  • @Jhecht the id 'list' is dynamically generated from the javascript, and that is working fine. The click events through the onload() event aren't working.
    – helpisgood
    Feb 12, 2017 at 4:02

1 Answer 1

1

You have to add atleast a single text inside ul tag. Then only you will be able to click it.

 window.addEventListener('load', function() {
   document.getElementById("list").addEventListener('click', function(event) {
     var target = getEventTarget(event);
   });
 });
5
  • 1
    This answer is useless. It's functionality equivalent to OPs code. Feb 12, 2017 at 4:02
  • Both are different
    – Sagar V
    Feb 12, 2017 at 4:05
  • 1
    No it's not different. Inline event handlers and using addEventListener do exactly the same thing. Here .onclick and .addEventListener('click', along with window.addEventListener('load' and window.onload do the same thing. Feb 12, 2017 at 4:11
  • Inline event have a limit. But addEventalistener not
    – Sagar V
    Feb 12, 2017 at 4:11
  • The code is still the same and doesn't work to solve OP's issue.
    – Jhecht
    Feb 12, 2017 at 4:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.