Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Uncaught TypeError: Cannot read property 'length' of undefined

 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<div> elements do not have values.
 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I figured that out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic