0

I have the following html structure created dynamically by foreach loop and tried to remove the whole element by accessing it from (ACTIVE HYPERLINK). I tried many different way, but cannot reach to it. As the whole block (of ACTIVE HYPERLINK) is repeated, I think it is meaningless to use class name of the hyperlink. I also tried to use a.active but not 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 some example of my tries:

$("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?

Jack
  • 1
  • 21
  • 118
  • 236
  • No element having `className` `"active"` appears at `html`? `var $ctrl = $('.xxx');` selects all `.xxx` elements, not only clicked `.xxx` element; `ctrl.closest('.aaa').remove();` would only remove first `.xxx` element. – guest271314 Oct 10 '16 at 19:10
  • I thought active like hover, but when debugging by Firebug and break, there seems to be no active class (I inspected the clicked hyperlink). – Jack Oct 10 '16 at 19:13
  • `:active` is a `css` pseudo class, not a `className` – guest271314 Oct 10 '16 at 19:28
  • 1
    It does not add a class. `:active` is a state between *click --- release*. Doesn't need `href` to be set. – AA2992 Oct 10 '16 at 19:28
  • @AnkithAmtange _"Doesn't need href to be set."_ ? How can `a` element be `:active` where no `href` is present as attribute? What would be `:active`? – guest271314 Oct 10 '16 at 19:29
  • 1
    Yeah. Its a state of user activation. Does not depend on href. [**MDN**](https://developer.mozilla.org/en/docs/Web/CSS/:active) .. You can apply it on a div element too for that matter. – AA2992 Oct 10 '16 at 19:31
  • @AnkithAmtange You are correct http://plnkr.co/edit/lBdOuwO4bZ8vcXpyVPch?p=preview – guest271314 Oct 10 '16 at 19:36
  • 1
    @AnkithAmtange Workaround to save the state? https://jsfiddle.net/u3uhq9m1/1/ – guest271314 Oct 10 '16 at 19:45
  • Can I get the active hyperlink (last clicked one) for this scenario? Any idea? – Jack Oct 10 '16 at 19:57
  • @ClintEastwood Yes, `javascript` at Answer should achieve this, as `this` is passed directly to `deleteRecord`. Is `$.fn.confirmation` defined at `javascript` at Question? Link to documentation? Have also composed a workaround. – guest271314 Oct 10 '16 at 20:12
  • @AnkithAmtange See http://stackoverflow.com/questions/39966288/how-to-pass-css-active-pseudo-element-to-javascript/39966289#39966289 – guest271314 Oct 10 '16 at 20:32