How to fix javascript console errors for elements that don’t exist
-
I have several div’s (with specific IDs) that, intentionally, exist for some users but not others.
Users A: These div’s appear
Users B: These div’s don’t appearThe issue is that javascript functions that use these div’s (by using their IDs) cause console errors for Users B:
Uncaught TypeError: Cannot set property ‘value’ of null
I’ve tried using the following if statement to fix the issue. I also tried adding “!= null” and “!= ‘undefined'” in the if statement, but that doesn’t solve it.
<script>if(document.getElementById(“my_id”)){document.getElementById(“my_id”).value = “Contents of the div go here”;}</script>
Any suggestions for how to prevent these console errors? Thank you
- The topic ‘How to fix javascript console errors for elements that don’t exist’ is closed to new replies.