Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of a JQuery selector can I use to select a link by data attribute?

I have the following HTML on my page:

<a data-href="/C003Q/History"><span>History</span></a>

Is there a JQuery selector that will allow me to send a click event to the link?


2 Answers

You can use attribute selector,

Live Demo

$('a[data-href="/C003Q/History"]').click(function(){        
   alert("clicked");
});​
like image 133
Adil Avatar answered Feb 02 '26 06:02

Adil


Use this method if you want to trigger the click event:

$('a[data-href="/C003Q/History"]').click();

Or use the following code if you want to pass a click handler to the link:

$('a[data-href="/C003Q/History"]').click(function(){
    console.log("click");
    //And do your logic here
});

This works for me.

like image 21
RTB Avatar answered Feb 02 '26 07:02

RTB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!