I’m doing a project and I can’t connect the API, inspecting the code I found that Document.getElementById is null but it’s not

Asked

Viewed 16 times

-1

In the HTML file I inserted the script of the JS file where my function is, so I can connect one file to another.

<script src="./main.js"></script>

In the cep input field the id="cep".

<label for="cep">CEP</label>
<input type="text" id="cep" required>

In the JS file I used the function document.getElementById('cep').addEventListener('focusout',pesquisarCep); but informs that it is void.

I don’t know where else to locate the problem.

  • If you call the element before it exists, it will be null. For example, if you call the JS in the document HEAD and the document.getElementById('cep') before the element cep exist on the page.

  • Thank you, I put it in the end and it worked.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.