How to get the link id with jquery

2

I have a grid and in the last column I will have a link that will be following format:

id.1

id.2

id.3

id.4

I would like to click on the link

<a id="id."'.$row["id"].' href="#">

Jquery identifies the id so I can call a feature. How do I get the click event from the link?

Getting the button is quiet

 $('#postar').click(function() {

How could I get the link click only by a piece of the "id"

$('#id').click(function() {
    
asked by anonymous 21.11.2016 / 23:14

2 answers

2

You can use as ricardolobo , using a selector that looks for elements whose ID begins with a given string. An example would be (jsFiddle):

$('[id^="id."]').on('click', function(){
	var nr = this.id.slice(3);
	alert(nr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><aid="id.1" href="#">Numero 1</a>
<a id="id.2" href="#">Numero 2</a>
<a id="id.3" href="#">Numero 3</a>

Another alternative would be to use the position of the elements, and read the index, without being connected to the ID. In this case it is worth mentioning that the index starts in 0 , so the first element would be the number 0 .

It would look something like this:

$('nav a').on('click', function() {
    var nr = $(this).index();
    alert(nr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><nav><aid="id.1" href="#">Numero 1</a>
    <a id="id.2" href="#">Numero 2</a>
    <a id="id.3" href="#">Numero 3</a>
</nav>
    
22.11.2016 / 09:04
1

You can use link

In this case for a id with the name id1 :

$('[id^=id1]')
    
21.11.2016 / 23:58