Jquery function click on dynamic PHP links

Asked

Viewed 770 times

0

I’m trying to create a script so when the person click Like she will like the post, and when you click Excuse, will forgive the post, obvious, I managed to do this successfully, but I had to do this by putting the jquery script in PHP, making each result, a new script was created, it would be too big. I wanted to leave only 1 code, as I do to get the IDS there in the case of dynamic results?

Let me explain briefly, why it got a little confused, I want to instead of having to put this javascript in the dynamic select, put only 1 code for all.

JQUERY

    $(document).ready(function(){
    // like and unlike click
    $("#like<?php echo $row['idd']; ?>, #unlike<?php echo $row['idd']; ?>").click(function(){

         var iconCarregando = $('<img src="loading.gif" width="70"> arregando. Por favor aguarde...');

                $('#statuslike<?php echo $row['idd']; ?>').html(iconCarregando);

            $.ajax({
        type: "GET",
        url: 'acoes.php?like=1&id=<?php echo $row['idd']; ?>',
        success: function(data){
      $(iconCarregando).remove();
    } });
    });
});

HTML

(the code is half, why can’t I put the full code)

a href="javascript:void(0);" id="like'.$msg_id.'" title="Like" rel="Like"><i class="fa fa-hand-peace-o margin-r-5"></i> Aplaudir
  • We need to be aware of what you’ve done and how you’re doing to help.

  • But I already put the.O there.

  • The way I did, and how I wanted it to be, I don’t know how to do...

2 answers

1


You do not need to call the click event by button ID. You can give a common class to everyone (e.g.. curtir) and capture all clicks there. After captured you use $(this) to identify which button was clicked. If you don’t have the ID information in another attribute, just break the ID by the known part. Ex.:

$(document).ready(function(){
    // like and unlike click
    $(".curtir").click(function(){ // captura todos os cliques de curtir

         var iconCarregando = $('<img src="loading.gif" width="70"> arregando. Por favor aguarde...');

        var ID = $(this).attr('id').split('like')[1]; // pega o ID e retira a parte "like"

                $('#statuslike'+ID).html(iconCarregando);

            $.ajax({
        type: "GET",
        url: 'acoes.php?like=1&id='+ID,
        success: function(data){
      $(iconCarregando).remove();
    } });
    });
});
  • i did as you said and put it in the html <a href="javascript:void(0);" class="like'. $msg_id. '" id="like'. $msg_id. '" title="Like" rel="Like"><i class="fa fa-hand-Peace-o margin-r-5"></i> Like</a> but nothing happens when you click

  • uses only class="curtir" don’t need the msg_id there

  • Friend does not yet : I tried to see the console and no error appears, what can be?

  • is it because he’s not getting the id? is where I think he knows what the id is??

  • there was a mistake there, you need the index [1], not [0]. I changed.

  • Thank you very much, it worked, thank you from my heart

Show 1 more comment

0

If the element was created dynamically use on of jQuery in the BODY element instead of direct click and of course insert this id in the element containing the clickable class ex:

<div>
   <span id="id-do-botao-curtir" class="classe_clicavel">curtir</span>
</div>

$(body).on('click', '.classe_clicavel', function(){

    var id = $(this).attr('id');

    // ex de teste ou sua lógica
    alert(id);
});

Browser other questions tagged

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