Unable to get parent using jQuery

I have the following html structure created dynamically using a foreach loop and trying to delete an entire element by accessing it from (ACTIVE HYPERLINK). I tried many different ways, but I can not reach it. Since the entire block (ACTIVE HYPERLINK) is repeated, I think it makes no sense to use the name of the hyperlink class. I also tried using a.active, but didn't seem to work.

@foreach (var file in Model.FileAttachments)
{
    <li class="aaa">
        <div class="bbb">
            <div class="ccc">
                <div class="ddd">                   
                    <div class="eee">
                        <ul class="fff">
                            <li>
                                <a class="xxx" href="javascript:void(0);" data-id="@file.Id" data-toggle="confirmation" ></a> <!-- ACTIVE HYPERLINK -->
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </li>
}

<script>

    $('.xxx').click(function (e) {
        e.preventDefault();
        $('[data-toggle="confirmation"]').confirmation('show');
    });


    $('[data-toggle="confirmation"]').confirmation({
        //code omitted for brevity      
        onConfirm: function () { deleteRecord(); }, // Set event when click at confirm button
    });


    function deleteRecord() {
        var $ctrl = $('.xxx');

        $.ajax({
        //code omitted for brevity

        success: function (response, textStatus, XMLHttpRequest) {
            if (response.success) {             
                ctrl.closest('.aaa').remove();
                //or                
                $("a.active").closest('.jFiler-item').remove();
            }
        });

    };

</script>

Here is an example of my attempts:

$("a.active").closest('.aaa').remove();
$(".xxx").closest('.aaa').remove();
$(this).data('.aaa')remove();
$("a.active").parents('li').eq(2)remove();
$(".xxx").parents('li').eq(2)remove();

Any idea?

0
source share
3 answers

UPDATED RESPONSE

$('.xxx').click(function (e) {
    var $this = $(this);
    e.preventDefault();
    $('[data-toggle="confirmation"]').confirmation({
        //code omitted for brevity      
        onConfirm: function () {
            deleteRecord($this); // send reference to delete method
        }
    });
    $('[data-toggle="confirmation"]').confirmation('show');
});

...

function deleteRecord($ctrl) {
...

ORIGINAL RESPONSE

, <a>, click:

$('.xxx').on('click', function(){
    $(this).closest('.aaa').remove();
});
+2

delagation, event.preventDefault() click <a>, ; event.target: this deleteRecord()

$(document).on("click", ".xxx", function (e) {
    e.preventDefault();
    var curr = $(this);
    curr.confirmation('show')
    .confirmation({
      // pass `curr` to `deleteRecord()`
      onConfirm: function () { deleteRecord(curr); }
    })
});


function deleteRecord(el) {

    $.ajax({
    //code omitted for brevity

    success: function (response, textStatus, jqxhr) {
        if (response.success) {             
            el.closest('.aaa').remove();
        }
    });

};
+2

Here is the final answer that works like a charm with @ZoliSzabo and guest271314 . Thank you very much for your help and suggestions ...

@foreach (var file in Model.FileAttachments)
{
    <li class="aaa">
        <div class="bbb">
            <div class="ccc">
                <div class="ddd">                   
                    <div class="eee">
                        <ul class="fff">
                            <li>
                                <a class="xxx" href="javascript:void(0);" 
    data-id="@file.Id" data-toggle="confirmation" ></a> <!-- ACTIVE HYPERLINK -->
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </li>
}



<script>

    var $ctrl = null; //define variables globally 
    var id = 0; //define variables globally 

    $('.icon-jfi-trash').bind('click', function (e) {
        e.preventDefault();
        $ctrl = $(this);
        id = $(this).data('id');
        $(this).find('[data-toggle="confirmation"]').confirmation('show');
    });


    $('[data-toggle="confirmation"]').confirmation({
        //code omitted for brevity
        onConfirm: function () { deleteRecord(); }
    });




    function deleteRecord() {
        var token = $('[name=__RequestVerificationToken]').val();

        $.ajax({
            type: "POST",
            url: '@Url.Action("DeleteRecord", "Controller")',
            cache: false,
            dataType: "json",
            data: { __RequestVerificationToken: token , id: id },

            success: function (response, textStatus, XMLHttpRequest) {
                if (response.success) {
                    $ctrl.closest('.jFiler-item').remove();
                }
            }
        });
    }

<script>
0
source

Source: https://habr.com/ru/post/1657321/


All Articles