Praveen Praveen - 1 year ago 158
Javascript Question

Cannot read property length of undefined

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) {

if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", walkmydog, false);

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download