0

document.getElementById("-1").click()
 <div tabindex="-1" id="-1" onclick="console.log('yes1')"></div>
    

Somehow document.getElementById("-1").click() throws the error Cannot read property 'click' of null.

Please help.

Satpal
  • 132,252
  • 13
  • 159
  • 168
atswann
  • 13
  • 1
  • 1
  • 6
  • I've edited your post to include a snippet of your code and it works. – George Dec 05 '16 at 13:40
  • Is that ID unique in the page DOM? – Meezaan-ud-Din Dec 05 '16 at 13:42
  • Use valid Identifiers see [What are valid values for the id attribute in HTML?](http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html) – Satpal Dec 05 '16 at 13:42
  • the html actually looks like this. when page is ran. i inspected the elements and the correct ids are set. but it still throws the error – atswann Dec 05 '16 at 15:21

1 Answers1

1

Your code works. Try to see where do you use click() function and see if they throw an error or not.

document.getElementById("-1").click();
<div tabindex="-1" id="-1" onclick="console.log('yes1')"></div>
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112