text() not working in IE8 -> undefined
I have difficulties with the text() function in IE8:
- <a href="#">Alex</a>
- <SCRIPT type=text/javascript>
- $(document).ready(function () {
- $('a').click(function () {
- alert('display text: ' + this.text);
- return false;
- });
- });
- </SCRIPT>
Basically what I want to do is to output the link's display text in an alert box when I click on the link.
This works in FF, Chrome and Opera - but not in IE8.
In FF, Chrome and Opera, the alert box would read "display text: Alex", in IE8 it reads "display text: undefined".
Have I done something wrong?
Thanks for all hints!
(BTW: the "real world" application is of course a little less academic: I want to present a list of options and on clicking on one of them, the respective text is filled into an input element somewhere else on the page - I just tried to cut the code down to a very placative example, that's why it may not look too useful)
[SOLVED:]
Below you can see the working version. The following changes were necessary:
- add parentheses to the text() method
- convert javascript object to a jQuery object to be able to access the text() method
Both changes are reflected in line 5:
- <a href="#">Alex</a>
- <SCRIPT type=text/javascript>
- $(document).ready(function () {
- $('a').click(function () {
- alert('display text: ' + $(this).text());
- return false;
- });
- });
- </SCRIPT>