Taking the data from the value field dynamically in Jquery

Asked

Viewed 24 times

-1

I’m having a hard time implementing a pop-up button in jquery... I have a user list that are logged in to admin screen the admin can move the user The problem is that the button only shows the first user from the list

 $('.comment-phara').on('click','#deslogar', function () {
    var select = $('#deslogar').val();
    alert(select);
    Lobibox.notify('info', {
        msg: 'Usuario deslogado já pode logar na estação.'
    });
});
                             <?php 
                                foreach ($logados as $usuarios){
                                    echo '<li class="lista">'
                                    . '<div class="comment-phara">'
                                            . '<div class="comment-adminpr">'
                                            . '<p class="dashtwo-messsage-title"  id="id_usuario"/>' . $usuarios['nome_completo'] . '</p>'
                                            . '</div>'
                                            . '<div class="admin-comment-month">'
                                            . '<p class="comment-clock"><i class="fa fa-clock-o"></i> Logou as 16:55</p>'
                                            . '<br>'
                                            . '<button class="btn btn-custon-four btn-primary btn-xs" id="deslogar" value="'. $usuarios['nome_completo'] .'">Deslogar</button>'
                                            . '</div>'
                                            . '</div>'
                                            . '</li>';
                                    
                                }
                                ?>

1 answer

0


There are two main problems in logic, the first is in creating an element with an id that will be repeated on screen, ideally ids should be unique in DOM. If you create elements in a repeat structure like foreach and need to add an id to any of them, try to include a key belonging to the driven object, in other cases preferably using classes.

The second problem is how you get the value of the item that was clicked, when using the command $('#deslogar').val() even if it were searching for a class, you are taking any element of the screen that corresponds to the search value, instead of using a term to search for the element, you can use the object this in this way:

$(this).val();

this is the object that corresponds to the element that had the event triggered.

  • Perfect bombed with the $(this). val(); As I use the same button saved me man!! I will use the class also.

Browser other questions tagged

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