Adjust this javascript to work with li in addition to tr

I have a javascript function that let's me do an ajax MVC callback to delete a record, then it removes the tr from the table to indicate it has been removed. The code works fine, but now I'd like to extend the script to work with ul/li items as well. Below is the setup that works. After that is the ul/li setup I'd like to extend the script to.

Here's the <tr>

<tr>
    ...
    <td>
        <a href="javascript:void(0)"
           onclick="ModifyCustomField('#createCustom','#customFieldEdit','@Url.RouteUrl("Default",
                new { action = "_CustomFieldEdit", id = item.CustomFieldId },
                Request.Url.Scheme)');">Edit</a> |
        <div class="delete-section">
            <a class="delete-link" href="@Url.Action("_CustomFieldDelete", new {id = item.CustomFieldId})">Delete</a>
            <div class="btn btn-primary delete-confirm" style="display: none"
                 data-delete-id="@item.CustomFieldId"
                 data-delete-controller="customforms"
                 data-delete-action="_customfielddelete">Confirm Delete</div>
        </div>
    </td>
</tr>

And the script:

$(Document).on("click","a.delete-link",function (event) {
    var deleteLink = $(this);
    deleteLink.hide();
    var confirmButton = deleteLink.siblings(".delete-confirm");
    confirmButton.show();

    var cancelDelete = function () {
        removeEvents();
        showDeleteLink();
    };

    var deleteItem = function () {
        removeEvents();
        confirmButton.hide();
        var url = '/' + confirmButton.attr('data-delete-controller') + '/' + confirmButton.attr('data-delete-action') + '/' + confirmButton.attr('data-delete-id');
        $.post(
            url,
            AddAntiForgeryToken({ id: confirmButton.attr('data-delete-id') }))
           .done(function () {
               var parentRow = deleteLink.parents("tr:first");
               parentRow.fadeOut('fast', function () {
                   parentRow.remove();
               });
           }).fail(function (data) {
               alert("error");
           });
        return false;
    };

    var removeEvents = function () {
        confirmButton.off("click", deleteItem);
        $(document).on("click", cancelDelete);
        $(document).off("keypress", onKeyPress);
    };

    var showDeleteLink = function () {
        confirmButton.hide();
        deleteLink.show();
    };

    var onKeyPress = function (e) {
        //Cancel if escape key pressed
        if (e.which == 27) {
            cancelDelete();
        }
    };

    confirmButton.on("click", deleteItem);
    $(document).on("click", cancelDelete);
    $(document).on("keypress", onKeyPress);

    return false;
});

The <li> items are set up like this:

<li class="editorRow ui-state-default">
    @using (Html.BeginCollectionItem("CustomFieldOptions"))
    {
        <div class="row">
            @Html.HiddenFor(model => model.CustomFieldOptionId)
            @Html.HiddenFor(model => model.CustomFieldId)
            @Html.HiddenFor(model => model.SortOrder, new { @class = "SortOrder" })
            @Html.HiddenFor(model => model.IsActive)

            @Html.ValidationMessageFor(model => model.OptionLabel, "", new { @class = "text-danger" })

            <div class="col-md-2">
                @Html.LabelFor(model => model.OptionLabel, htmlAttributes: new { @class = "control-label" })
            </div>
            <div class="col-md-7">
                @Html.EditorFor(model => model.OptionLabel, new { htmlAttributes = new { @class = "form-control" } })
            </div>
            <div class="col-md-3">
                <a class="delete-link" href="@Url.Action("_OptionEditorRowDelete", new {id = Model.CustomFieldOptionId})">Delete</a>

                <input type="button" value="Delete" class="btn delete-link" />
                <div class="btn btn-primary delete-confirm" style="display: none"
                     data-delete-id="@Model.CustomFieldOptionId"
                     data-delete-controller="customforms"
                     data-delete-action="_OptionEditorRowDelete">Confirm Delete</div>
            </div>
        </div>
    }
</li>

I was thinking I could change this:

$(Document).on("click","a.delete-link",function (event) {
    //...
    var parentRow = deleteLink.parents("tr:first");

to this:

$(Document).on("click",".delete-link",function (event) {
    //...
    var parentRow = deleteLink.parents("tr:first li:first");

But I'm not sure about the 'parents' line.

Answers


I'd just add the same class to both - li:

<li class="my-item editorRow ui-state-default">
    ...
</li>

and tr:

<tr class="my-item">
    ...
</tr>

And then use jQuery's .closest() method:

var parentRow = deleteLink.closest(".my-item");

Just add a comma to the selector like Lauromine suggests. Should work then.

var parentRow = deleteLink.parents("tr:first, li:first");


Need Your Help

Why the program not run correctly?

html asp-classic

I am beginner to html and asp. I should write code of the program in Notepad. I want the program that when I click on the Random Button, generate a random number and when I click on the Check butto...

How do I run delayed job inserts in the backgroud without affecting page load - Rails

ruby-on-rails ruby delayed-job

I have an RoR application like posting answers to a question. If a user answers to a question, notification messages are sent to all the users, who watch-listed the question, who tracks the questio...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.