0

I am trying to active .click on something and it's just not working.

example HTML :

<input type="text" id="food">

js :

$('#food').click( () => {
    console.log('working');
})

I tried to use addeventlister without jquery and I am getting this:

Uncaught TypeError: Cannot read property 'addEventListener' of null at script.js:11

code :

const foodEL = document.getElementById('food');
foodEL.addEventListener( 'click', () => {
    console.log('working');
})

try it https://github.com/Dor-Elisha/RecipeSearcher.git

Dor Elisha
  • 43
  • 6
  • You are running your code before the element exists, hence why you get the error when using addEventListener. jQuery doesn't error out when it doesn't find the element – Patrick Evans Aug 22 '21 at 23:29
  • Try to move the ` – Krzysztof Antosik Aug 22 '21 at 23:29
  • Another fix around this problem is to wrap the contents of your `script.js` into `$(function() { /* your code here */ });` (shorthand for [`.ready()`](https://api.jquery.com/ready/)). If you use this wrapper, the code will be executed when the entire page DOM has been parsed, and your `` is part of DOM at that point. – tao Aug 22 '21 at 23:34

0 Answers0