Cannot read property ‘toLowerCase’ of undefined

javascript error undefined

I am finally back with another post after a while. I had this time period where I got busy with other things in life and felt a little tired for coding and blogging. That seems to be over now. Thank you my little angel. 🙂

Today I was working on my Django project and had this annoying error in my javascript code. My code kept saying Cannot read property ‘toLowerCase’ of undefined but why? What was happening?

Apparently the problem was with the context of the function I was calling and the meaning if “this” parameter inside that function.

I was trying to invoke a function attached to the “onchange” event of a select control. I was attaching the function to the event “inline” as below:

Nothing wrong with that. The problem was that the function “myFunction” was defined in the scope of the page and I was using “this” in that function as if it referred to the select control itself while it was actually referring to the page.



Instead of using “this”, I passed a parameter to myFunction and built the id of my select control to use it instead of “this”. The problem was gone.

If you are seeing the same error message you might have a similar problem in your code.

Hope this helps.
Good Luck,