Attribute index value

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 href id when the user clicks on it. Something like reading [1] read [2], etc.

+4
source share
2 answers
 $('a').click(function( e ) { alert(this.id); // e.preventDefault(); // Uncomment this line if you don't want }); // to follow the link 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 link from default behavior (after it href ).

It would probably be better to add a class attribute to the links and select with this:

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

+5
source

Here you go

 $('a[id^=read]').click(function(){ alert(this.id); return false; }); 

I use the attribute-starts-with selector to target links with id that starts with read

+3
source

All Articles