15

Possible Duplicate:
jQuery 1.7 - Turning live() into on()

//Solution: I simply replaced the four occurrences like the approved answer suggested and unobtrusive ajax plugin is up and working again with jquery 1.9.0

Update//Observe the comments for the answer marked at the bottom which is the best way to solve this.

//Original post: I upgraded to jQuery 1.9.0 but then unobtrusive ajax plugin went down since they deprecated the live method. I tried to replace it like this since the upgrade fixes an other bug for me. However, it does not work. I simply replaced live with on like this:

$("a[data-ajax=true]").on("click", function (evt) {
        evt.preventDefault();
        asyncRequest(this, {
            url: this.href,
            type: "GET",
            data: []
        });
    });

    $("form[data-ajax=true] input[type=image]").on("click", function (evt) {
        var name = evt.target.name,
            $target = $(evt.target),
            form = $target.parents("form")[0],
            offset = $target.offset();

        $(form).data(data_click, [
            { name: name + ".x", value: Math.round(evt.pageX - offset.left) },
            { name: name + ".y", value: Math.round(evt.pageY - offset.top) }
        ]);

        setTimeout(function () {
            $(form).removeData(data_click);
        }, 0);
    });

    $("form[data-ajax=true] :submit").on("click", function (evt) {
        var name = evt.target.name,
            form = $(evt.target).parents("form")[0];

        $(form).data(data_click, name ? [{ name: name, value: evt.target.value }] : []);

        setTimeout(function () {
            $(form).removeData(data_click);
        }, 0);
    });

    $("form[data-ajax=true]").on("submit", function (evt) {
        var clickInfo = $(this).data(data_click) || [];
        evt.preventDefault();
        if (!validate(this)) {
            return;
        }
        asyncRequest(this, {
            url: this.action,
            type: this.method || "GET",
            data: clickInfo.concat($(this).serializeArray())
        });
    });
Community
  • 1
  • 1
Magnus Karlsson
  • 3,549
  • 3
  • 31
  • 57
  • You could start by searching stackoverflow or reading the documentation. http://stackoverflow.com/questions/8021436/jquery-1-7-turning-live-into-on http://api.jquery.com/on/ – Kevin B Jan 18 '13 at 18:26
  • 13
    @KevinB a bit harsh and unhelpful :/ I hit the same issue and Googled and search Stackoverflow - I hit this question and never came across the one you linked to – Cocowalla Jan 18 '13 at 19:48
  • I guess some people just have trouble coming up with good search terms. For example, this one was "convert .live to .on jQuery", the first 6 results i get all show good examples of how to convert `.live` to `.on`. – Kevin B Jan 18 '13 at 19:52
  • 7
    I guess I could have formulated my question better but my intention was that others searching for problems with unobtrusive ajax would find this answer. Not everyone with a broken unobtrusive ajax knows its .find that has been deprecated. – Magnus Karlsson Jan 18 '13 at 19:58
  • 1
    This question is specific to jQuery Unobtrusive Ajax whereas the proposed duplicate is not. The solution here is to get the latest version of jQuery Unobtrusive Ajax (it has been fixed to use `.on()`), not to manually modify it. – JLRishe Dec 13 '14 at 18:14

1 Answers1

12

Equivalent of live using on (delegation) is:

$(document).on("click","a[data-ajax=true]", function (evt) {...});

You can find jquery's .on() method documentation here:

>> http://api.jquery.com/on/ <<

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. For help in converting from older jQuery event methods, see .bind(), .delegate(), and .live().

To remove events bound with .on(), see .off(). To attach an event that runs only once and then removes itself, see .one()

A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • also check the workarounds here - http://connect.microsoft.com/VisualStudio/feedback/details/776965/please-support-jquery-v1-9-0-properly-in-jquery-validate-unobtrusive – Simon_Weaver Feb 05 '13 at 05:07
  • 2
    and upgrade jquery.validate to 1.11.0 (released today) : https://nuget.org/packages/jQuery.Validation/1.11.0 – Simon_Weaver Feb 05 '13 at 05:08
  • There is now an even newer version of jquery.validate : https://www.nuget.org/packages/jQuery.Validation/ – Chris Sep 08 '13 at 10:04