Reputation:
I have a div that has been included into my page dynamicly so I don't have access to it as it's not on my current page.
This is how it looks:
<h2 class="title">About</h2>
What I need to do is to add an 'onclick' event that targets this div.
All I know is that it's a 'h2' with a class of 'title' and with text containing 'About'.
How can I do this?
Upvotes: 0
Views: 1273
Reputation: 8346
You can use .filter
for specific match.
As :contains
will match <h2 class="title">About Us</h2>
as well.
$("h2.title").filter(function () {
return $.trim($(this).text()) == "About";
}).click(function(e) {
});
Upvotes: 0
Reputation: 61
Use this:
$(document).on('click','h2.title', function(e){
if(e.currentTarget.textContent.trim() == "About") {
//write your code here
}
});
Upvotes: 0
Reputation: 26
you can use :contains to select also contained text like this:
$("h2.title:contains('About')").click(function(e) {
// Handle here
});
Upvotes: 1