Reputation: 3599
So I'm working on a dashboard (one page) type application, and I have a set of links:
<a href = "#">3s01</a>
<a href = "#">5e02</a>
<a href = "#">2k01</a>
<a href = "#">3a01</a>
<a href = "#">5j01</a>
etc...
The way the text in between the tags is populated is by grabbing the data from an xml file using JQuery. Because the xml file can change, the links aren't static. My task is to be able to click on a link and figure out what the text between the tags is, so that I can populate a table on the page accordingly. The only problem is I don't know how to get that data using JQuery/Javascript/html/whatever. Any help would be much appreciated!
Upvotes: 2
Views: 123
Reputation: 87073
$('a[href=#]').on('click', function(e) {
e.preventDefault();
alert($(this).text());
});
But if you a
are dynamically generated that means, generate after DOM ready then you should try
$('body').on('click', 'a[href=#]', function(e) {
e.preventDefault();
alert($(this).text());
});
Here instead of body
you can use ANY Valid Selector
that is parent of a
tags which belongs to DOM already.
Read more about .on()
Upvotes: 1
Reputation:
Use this
in the handler.
$('a[href="#"]').click(function() { alert(this); });
To get the text, use .text()
...
alert($(this).text());
Or the not officially supported yet faster version, $.text()
...
alert($.text([this]));
Upvotes: 2