How to get the link id with jquery

Asked

Viewed 1,491 times

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 identify the id so I can call a feature. How can I catch the event from clicking the link?

Picking from the button is quiet

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

How could you catch the link click only by a piece of "id"

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

3 answers

2


You can use it as the ricardolobo suggested, using a selector that searches for elements whose ID starts with a given string. An example would look like this (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>
<a id="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 linked to the ID. In this case it is worth mentioning that the index starts at 0, so the first element would be the number 0.

It would be something like that:

$('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>
    <a id="id.1" href="#">Numero 1</a>
    <a id="id.2" href="#">Numero 2</a>
    <a id="id.3" href="#">Numero 3</a>
</nav>

1

  • Ricardo, my id=1 or id=2 or id=3 how would I get only the number after the ID?

  • Ricardo, I got this: var id = $(this). attr('id');

0

function listarAtivos(user) {

    let dados

    // faço o teste , comparando a lista de id que esta vindo com o que esta em acesso
    if (user.id != usuario.id) {
        // so vai imprimri caso não seja igual ao usuario logado


        dados = '<div class="card w-75">'
        dados += '<div class="card-body">'
        dados += '<input type="hidden" id="titulo_escondido" name="titulo_escondido" value=' + user.apelido + '>'
        dados += '<h5 class="card-title">' + user.apelido + '</h5>'
        dados += '<input type="hidden" id="codigo_escondido" name="codigo_escondido" value=' + user.id + '>'
        dados += '<p class="card-text">Deseja bater papo com você.</p>'
        dados += '<button type="button" id="aceitar_chat" value=' + user.id + ' class="btn btn-primary" data-toggle="modal" data-target="#modal_chat">Vamos ?</button>'
        dados += '</div>'
        dados += '</div>'

        // coloca as informações na div
        $('#div_central').append(dados)

    }
}

For this case I used so . I created a card and it has the button that calls my modal , then I put the value on this button then I get the click

 $('body').on('click', '#aceitar_chat', function(e) {
            e.preventDefault()
                // esse id e capturado com base no valer que eu passo no botao
            let id = $(this).val()


        })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.