| 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: 56192
|
|
|
<div> elements do not have values.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About 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: 56192
|
|
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.
|
 |
 |
|
|
subject: Uncaught TypeError: Cannot read property 'length' of undefined
|
|
|