Search code examples
javascriptjquerydatatablessweetalertsweetalert2

How can I do get value in Sweet Alert Poppup Message Content?


I use JQUERY Datatable js and SweetAlert.js

There is a delete icon in datatable when click id get id and showing sweet alert confirm poppup . when click Yes it deleted fine working.

I want to get information about id’s = "TITLE" value to the warning message content.

DATATABLE

<tbody>
     <tr>
       <td>@item.TITLE</td>
       <td>
       **<a href="javascript:void(0);" onclick="deleteuser('@item.id')"><i class="fa fa-trash-o"></i></a>**
       </td>
     </tr>
 </tbody>

SWEETALERT Js code This Point get need here (bold marked value) => text: "@TITLE are u want delete!" id title text should come here

function deleteuser(userid) {
    swal({
        title: "Are u sure?",
        text: "@TITLE are u want delete!",
        icon: "warning",
        buttons: {
            cancel: {
                text: "No, iptal!",
                value: null,
                visible: true,
                className: "",
                closeModal: false,
            },
            confirm: {
                text: "Yes, sil!",
                value: true,
                visible: true,
                className: "",
                closeModal: false
            }
        }
    })

Solution

  • you can achieve that like this:

    <tbody id="my-table">
         <tr>
           <td class="title">Some Title</td>
           <td>
           <a href="javascript:void(0);" class="delete-btn" data-id="@item.id"><i class="fa fa-trash-o"></i></a>
           </td>
         </tr>
     </tbody>
    

    Later on in javascript:

    $('#my-table').on('click', '.delete-btn', function(){
    var deleteBtn = $(this),
    id = deleteBtn.data('id'),
    title = deleteBtn.closest('tr').find('.title').text();
    
    swal({
            title: "Are u sure?",
            text: title + " are u want delete!",
            icon: "warning",
            buttons: {
                cancel: {
                    text: "No, iptal!",
                    value: null,
                    visible: true,
                    className: "",
                    closeModal: false,
                },
                confirm: {
                    text: "Yes, sil!",
                    value: true,
                    visible: true,
                    className: "",
                    closeModal: false
                }
            }
        })
    });