• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Uncaught TypeError: Cannot read property 'length' of undefined

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<div> elements do not have values.
 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I figured that out.
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic