0

I am trying to alert something but on click function is running only once on the first button. but I have buttons on many rows.

I am fetching Data through Laravel from Database in a table. Only one button runs a function, then nothing happens with other buttons.

Jquery:

jQuery(document).ready(function(e)  {
    $('#restore-data').on('click', function(e) {
    var val = $("#thisr").attr('value');
    alert(val);

    });
});

View:

<table id="recover-table" class="table" >
    <thead>
    <tr class="bg-info">
        <th>Teacher Name</th>
        <th>Date</th>
        <th>Action</th>
    </tr>
    </thead>
    <tbody>

    @foreach($trashs as $trash)

    <tr id="thisr" value="{{$trash->id}}">
    <td class="text-nowrap ">{{$trash->efirst}} {{$trash->esecond}}</td>

    <td class="text-nowrap ">{{$trash->deleted_at}}</td>

    <td class="text-nowrap "><button type="submit"  class="" name="id" 
    value="{{$trash->id}}" id="restore-data">Delete</button></td>

    </tr>

    @endforeach </tbody></table>

Right now even alert is not working, but I want to achieve Refresh table after a record is deleted from Table.

Update: Now Alert is working fine, but when I delete a record by pressing a button, only one row is deleting. the function runs once.

Complete Js:

jQuery(document).ready(function(e)  {

    $('#restore-data').on('click', function(e) {

    let teacher_id=$(this).attr('value');
    console.log('Restore button clicked!')
    e.preventDefault();

    $.ajax(
    {
      url: "/teachers/recover/" + $('#restore-data').attr("value"),
      type: 'GET', // Just delete Latter Capital Is Working Fine
      headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
      data: teacher_id,

     success: function (data) {
     console.log(data.msg);
     console.log(teacher_id);

     if(data.msg){
         $('#thisr').remove();
         $('#response').empty();
         $(".toast").toast('show');
         $('#response').append(data.msg);
      }
    },
    error: function (xhr) {
    console.log("Error Restoring Record");
    //console.log(xhr.responseText);
    },
   });
  });
});

3 Answers 3

3

You can try to use class 'restore-data'

$(document).ready(function(e)  {
    $(document).on('click', '.restore-data', function(e) {
        var val = $('#thisr').val();
        alert(val);
    });
});

As id should be unique for each element.

You can try something like

@foreach($trashs as $trash)

<tr>
<td class="text-nowrap ">{{$trash->efirst}} {{$trash->esecond}}</td>

<td class="text-nowrap ">{{$trash->deleted_at}}</td>

<td class="text-nowrap ">
    <button type="button" data-trash-id="{{$trash->id}}" class="restore-data">Delete</button>
</td>

</tr>

@endforeach

and JS as

$(document).on('click', '.restore-data', function(e) {
    var trash_id = $(this).attr('data-trash-id');
    alert(trash_id);
});
Sign up to request clarification or add additional context in comments.

5 Comments

please check my edited answer last portion. if you can help thanks.
I have updated the answer, You need to use unique ids, you can use laravel blade loop variables
deleting only first row.
Please let me know if the alert shows the correct value?
As you can add attr to the button there is no need for them on tr. I have added data-trash-id attribute. If you need to remove parent tr tag then you can use $(this).parent('tr').remove(); or $(this).closest('tr').remove();
3

Change this line.

var val = $("#thisr").attr('value');

to (since you have value attribute in button):

var val = $(this).attr('value');

or (since you have value attribute td):

var val = $(this).parent('tr#thisr').attr('value')

To remove a row.

$('#restore-data').on('click', function(e) {

 var _this = $(this);
 ...

if(data.msg){
  _this.parent('tr#thisr').remove();
....

Also change button type to button.

<button type="button"  class="" name="id" 
    value="{{$trash->id}}" id="restore-data">Delete</button></td>

8 Comments

Thank you, it's working for alert, but how can i reload only table after alert.
please check my edited answer last portion. if you can help thanks.
still running only once. please check
added _this.parent('tr#thisr').remove(); but still only one row removed, not second. :(
i added alert(1) in start and that too runs once.
|
1

You gave same id to all button and id must be unique of particular button so you can define unique id with key of array and pass it to Function

@foreach($trashs as $key=>$trash)

<tr id="thisr{{$key}}">
    <td class="text-nowrap ">{{$trash->efirst}} {{$trash->esecond}}</td>
    <td class="text-nowrap ">{{$trash->deleted_at}}</td>
    <td class="text-nowrap ">
        <button type="button"  class="" name="id" value="{{$trash->id}}" id="restore-data{{$key}}" onclick="restoreData({{$key}})">Delete</button>
    </td>
</tr>
@endforeach
function restoreData(key){
    var val = $("#restore-data"+key).attr('value');
    alert(val);
    // you can use either
    $("#restore-data"+key).closest('tr').remove();
    // OR
    $('#thisr'+key).remove();
}

9 Comments

please check my edited answer last portion. if you can help thanks.
Thankyou so much can you modify code to delete rows?
try last line of the function i modified to delete row
i am using $('#restore-data').on('click', function(e), how can i pass key in it instead of using onclick.
Check this line <button type="button" class="" name="id" value="{{$trash->id}}" id="restore-data{{$key}}" onclick="restoreData({{$key}})">Delete</button>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.