Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property length of undefined

Tags:

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); } 
like image 821
Praveen Avatar asked Aug 23 '11 08:08

Praveen


People also ask

Can Read properties of undefined?

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.

Can not read property find of undefined?

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.

Can not read property replace of undefined?

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.

Can not read property split of undefined?

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.


1 Answers

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.

like image 70
GolezTrol Avatar answered Sep 22 '22 20:09

GolezTrol