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

this.title returning undefined

 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

When I click one of the cells of a table that I have on an HTML page, my javascript function is called.

One of the function's tasks is to assign the contents of the title attribute of whatever element was clicked to call the function, (in this case a table cell), to a variable.

I am using:



Whenever I test to see what has been assigned to the variable, I get 'undefined'. Does anyone know what I might be doing wrong?

Many thanks

Joe
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are going to need to see more code than that.

If I had to guess, sounds like your scoping is off.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the clicked element have a title attribute?
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,

Thanks for your reply. In order to isolate the problem, I made a separate html page and javascript script, I still get the same response. Here is my HTML:



And here is my javascript:



Regards

Joe
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do this instead



and



Eric
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed.

Carefully read this: http://www.quirksmode.org/js/this.html
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,

I implemented what you wrote and it's working great.

Thanks very much for your time, again. I appreciate it.

Regards

Joe
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of the reasons that assigning click handlers in the HTML markup can trip you up. You might want to look into the concept of Unobtrusive JavaScript.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic