Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The JS file doesn't work in partial view after it refreshed

This is branch question of "Js file not loaded after partial view is refreshed". The broblem is that if I put my script in to the main view it doesn't work in partial. My custom script:

$(function() {
    $.ajaxSetup({ cache: false });
    var timer = window.setTimeout(function () {
        $(".alert").fadeTo(1000).slideUp(1000, function () {
            $(this).hide();
        });
    }, 3000);
    $("[data-hide]").on("click", function () {
        if (timer != null) {
            clearTimeout(timer);
            $(this).closest("." + $(this).attr("data-hide")).hide();
        }
    });
});

The Photo partial view where I need to use my script:

<div class="well">
    <h3>
        <strong>@Model.Name</strong>
        <span class="pull-right label label-primary">@Model.AverageRaiting.ToString("# stars")</span>
    </h3>
    <span class="lead">@Model.Description</span>
    @Html.DialogFormLink("Update", Url.Action("UpdatePhoto", new {id = @Model.PhotoId}), "Update Photo", Url.Action("Photo"))
    @Html.Action("InitializeAlerts")
</div>

And partail view "_Alert" which rendering in to the partial where I need to use the script above:

@{
    var alerts = TempData.ContainsKey(Alert.TempDataKey)
                ? (List<Alert>)TempData[Alert.TempDataKey]
                : new List<Alert>();

    if (alerts.Any())
    {
        <hr />
    }
    foreach (var alert in alerts)
    {
        var dismissableClass = alert.Dismissable? "alert-dismissable" : null;
        <div class="alert [email protected] fade in @dismissableClass">
            @if (alert.Dismissable)
            {
                <button type="button" class="close" aria-label="close" data-hide="alert">&times;</button>
            }
            @Html.Raw(alert.Message)
        </div>
    }
}
like image 669
user3818229 Avatar asked Dec 24 '15 10:12

user3818229


2 Answers

Use a delegated event handler:

$(document).on('click', "[data-hide]", function () 

The jQuery selector is only run at event time, so it will work with dynamically added elements.

It works by listening for the event at a non-changing ancestor element (document is the safest default if nothing else is closer/convenient). It then applies the selector to the elements in the bubble-chain. It then applies your event handler function to only the matching elements that caused the event.

This is very efficient compared to connecting event handlers to individual elements and any speed difference is negligible as you simply cannot click fast enough to notice :)

like image 171
Gone Coding Avatar answered Nov 10 '22 02:11

Gone Coding


This happens as you have removed the DOM elements that jQuery attached its events to. jQuery doesn't keep track of the page and add new events when you add new elements, its a one time deal. You can get around this by placing the event on a parent that always exists on the page and then use a selector to get at the elements you want. The jQuery documentation for the .on() method tells you more.

I need to modify code as follows:(working well if partial view refreshed)

var item_to_delete;
    $("#serviceCharge").on('click','.deleteItem' ,function (e) {
        item_to_delete = $(this).data('id');
        alert(item_to_delete);
    });

My Previous code was: (working till partial view not refresh)

var item_to_delete;
        $(".deleteItem").click(function (e) {
            item_to_delete = $(this).data('id');
        });
like image 27
RAVI VAGHELA Avatar answered Nov 10 '22 00:11

RAVI VAGHELA