Skip to content
Advertisement

How to get caller element using JavaScript?

I have a table with id “tbl” and it has 2 rows and 3 cols. Each cell (td) has an explicitly defined id. Also, the table has an onclick event that calls a function foo(). The onclick() would be generated whenever any cell is clicked. The table tag is:

JavaScript

I also tried javascript:foo(this)

I want to find out the id of the table cell that was clicked.

I have tried the following JavaScript:

JavaScript

This works great in Google Chrome and IE, but not in Firefox. In Firefox, sender is undefined. How to get the caller cell in Firefox?

Advertisement

Answer

Firstly, remove javascript: from the onclick attribute. You’re confusing this with javascript in the href attribute of a link. In Javascript code, javascript: would create a label named “javascript”.

Other than that, foo(event) should work correctly with your final JavaScript code sample. The reason it doesn’t work in Firefox but does in Chrome and IE is; they both support the global event object, window.event (which is evaluated when your e.target yields undefined, because this is an element which will not have a target property). Firefox doesn’t support the global event object.

Further reading:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement