File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Uncaught TypeError: Cannot read property 'length' of undefined Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Uncaught TypeError: Cannot read property Watch "Uncaught TypeError: Cannot read property New topic
Author

Uncaught TypeError: Cannot read property 'length' of undefined

R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
Hi everybody,
In the following piece of code, I create two elements: title and left.



At this point, I check to see whether these nodes exist or not; they exist and are not null:



However, I receive this error as soon as the script comes to this line: Uncaught TypeError: Cannot read property 'length' of undefined
Looks like the value is not defined for them or its too early to check for the value property?



On the website, I see the fields with the "values" as follows; meaning it does show the values:

TITLE: Auto Body and Paint --> "Auto Body and Paint": is the value
35 characters left --> "35" is the value

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

<div> elements do not have values.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
Thanks so much. I modified the script to check the title.id and left.id instead of the id of the <div> element. The error is gone, and I don't receive any other error, however, I see the following when the alert() is executed:

Title: found: undefined
Left: found: undefined

Now my elements are found but I expected to see their values: "Auto Body and Paint" and "35". Why they are undefined?

R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
If I go to the Chrome JavaScript debugger, this is what I have:



I think first I need to refer to the input objects that I have - the following objects or elements - and then get the value from the object:



If my assumption is correct, I have difficulty referring to those objects within the code. I need to find out how can I refer to these two elements, b/c need to pass these elements to other function which then the function tries to access the values: "Auto Body and Paint" and "35".
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Your problem is not in accessing the elements but in the assumption that they have a value property. I've already told you: <div> elements do not have values.

If you're look for the contents, that'll be via the innerHTML property.

Get yourself a good JavaScript reference so you can see what the actual properties on the elements are rather than guessing.
R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
Thanks, I figured that out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uncaught TypeError: Cannot read property 'length' of undefined
 
Similar Threads
struts-2 (set values to action class)
Attach eventListener to objects
Calling a JavaScript function from within another JavaScript function
"Unterminated &lt;c:out tag" Error
Need Help With Dynamic text box in JSP