0

I've got this type of link in my HTML :

Librairy :

<script type="text/javascript" src="../../resources/js/jquery/jquery-1.12.3.min.js"></script>

HTML :

<a id='1' href='#' class='bulletinLink'> Bulletin du 11-01-2015 </a>
<a id='2' href='#' class='bulletinLink'> Bulletin du 13-02-2015 </a>
...

I want to get the id of this link when I click, this is my jQuery:

$(function() {
$('.bulletinLink').on('click',function(event){
    var id = $(this).attr('id');
    alert(id);
})
});

When I click in the link, it doesn't fire the jQuery function, what am I missing?

Pasja95
  • 93
  • 1
  • 15

3 Answers3

1

You could use the following simplified version, where you use this.id to retrieve the attribute id for your DOM without re-querying it.

PS: Make sure you have included jQuery in your page. Example:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

$(function() {
  $('.bulletinLink').on('click', function(event) {
    alert(this.id);
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id='1' href='#' class='bulletinLink'> Bulletin du 11-01-2015 </a><br>
<a id='2' href='#' class='bulletinLink'> Bulletin du 13-02-2015 </a>
GibboK
  • 71,848
  • 143
  • 435
  • 658
1

It is working fine with following code

$(function() {
  $('.bulletinLink').on('click', function(event) {
    alert($(this).attr("id"));
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id='1' href='#' class='bulletinLink'> Bulletin du 11-01-2015 </a><br>
<a id='2' href='#' class='bulletinLink'> Bulletin du 13-02-2015 </a>
Hemal
  • 3,682
  • 1
  • 23
  • 54
  • 1
    @Henak sorry, but why do you require the DOM using $(this)? It is not really necessary IMO. – GibboK Feb 02 '17 at 14:05
0

you need to add jQuery library...

Use this one :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Vishal Ghantala
  • 86
  • 1
  • 16