Niels Hermann
Niels Hermann

Reputation: 538

Change var to data-id value with click on button

I have this link:

<a class="PQReport" href="#" data-id="<?php echo $rowA['pq_id']; ?>"></a>

When I click that link I wan't the id of this button to change:

<button class="BorderBtn AskForDelete" id="">Delete</button>

When I click that button I wan't this script to work:

$(document).ready(function(){
    $('.AskForDelete').click(function() {
        var id = $('.AskForDelete').attr('id');
        $.ajax({
          type: 'post',
          url: 'delete.php',
          data: {id:id},
          dataType: 'html',
            success: function(data) {

            }
         });
        return false;
    });
});

The thing is that each link open a popup with that button (all on the same page - so no worries). Instead of making a popupbox for each link I would like to be able to just make 1 popup box that changes values depending of the link that is being clicked on.

I have tried all sorts of things - can't get it to work.

Any advice or pointers?

Thank you.

Upvotes: 0

Views: 64

Answers (1)

Unamata Sanatarai
Unamata Sanatarai

Reputation: 6637

Capture the click on .PQReport. Take it's data-id and put it in .AskForDelete's id. For example:

$(document).ready(function(){

$('.PQReport').click(function(e){
e.preventDefault();
    $('.AskForDelete').attr('id', $(this).data('id'))
})

    $('.AskForDelete').click(function(e) {
        e.preventDefault();
        var id = $('.AskForDelete').attr('id');
         
        console.log(id);
        $.ajax({
          type: 'post',
          url: 'delete.php',
          data: {id:id},
          dataType: 'html',
            success: function(data) {

            }
         });
        return false;
    });
    
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="PQReport" href="#" data-id="<?php echo $rowA['pq_id']; ?>">CLICK ME</a>


<button class="BorderBtn AskForDelete" id="">Delete</button>

Upvotes: 1

Related Questions