I am trying to simply check if I have an empty input text box but I get this error when I run this in Chrome:
Uncaught TypeError: Cannot read property 'length' of undefined.
Here is how I go about doing it. I check for DOM readiness and then call the function:
function walkmydog() { //when the user starts entering if(document.getElementById('WallSearch').value.length == 0) { alert("nothing"); } } if (document.addEventListener) { document.addEventListener("DOMContentLoaded", walkmydog, false); }
The "Cannot read properties of undefined" error occurs when trying to access a property on an undefined value. You often get undefined values when: accessing a property that does not exist on an object. accessing an index that is not present in an array.
The "Cannot read property 'find' of undefined" error occurs when the find() is called on an undefined value. To solve the error, make sure to only call the method on arrays.
The "Cannot read property 'replace' of undefined" error occurs when calling the replace() method on an undefined value. To solve the error, provide a fallback for the value if it's equal to undefined and conditionally check that it stores the correct data type before calling the method.
The "Cannot read property 'split' of undefined" error occurs when trying to call the split() method on a variable that stores an undefined value. To solve the error, make sure to only call the split method on strings.
The id of the input seems is not WallSearch
. Maybe you're confusing that name
and id
. They are two different properties. name
is used to define the name by which the value is posted, while id
is the unique identification of the element inside the DOM.
Other possibility is that you have two elements with the same id. The browser will pick any of these (probably the last, maybe the first) and return an element that doesn't support the value
property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With