I am using the following code ... ...
for($i=0; $i<90; $i++){
?>
<a id='read[<?php print $i; ?>]' href="<?php print $textToshow; ?>"> Text Shown</a>
<?php } ?>
I want to know the id of the a href when a user clicks on it. Something like read[1] read[2] etc
$('a').click(function( e ) {
alert(this.id);
// e.preventDefault(); // Uncomment this line if you don't want
}); // to follow the link's href.
This assigns a click
event to all <a>
elements that will alert its ID when clicked.
Uncomment the e.preventDefault()
line to prevent the default behavior of the link (following its href
).
It would probably be best to add a class attribute to the links, and select using that:
$('a.someClass').click(function( e ) {
alert(this.id);
// e.preventDefault(); // Uncomment this line if you don't want
});
This selects <a>
elements with the class "someClass"
using the class selector.
Here you go
$('a[id^=read]').click(function(){
alert(this.id);
return false;
});
I use the attribute-starts-with selector to target the links that have an id
that starts with read
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With