Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the index value of attribute

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

like image 912
user512438 Avatar asked Feb 26 '23 19:02

user512438


2 Answers

$('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.

like image 165
user113716 Avatar answered Mar 11 '23 05:03

user113716


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

like image 29
Gabriele Petrioli Avatar answered Mar 11 '23 04:03

Gabriele Petrioli