Tom Rider
Tom Rider

Reputation: 2815

how to use on event

I am trying to use on event. Code

$(".draggable").on("mouseover", function () {
                if (!$(this).data("init")) {
                    $(this).data("init", true).draggable();
                }
});

but the above code is not working. The draggable plugin is not apply on elements. Now if i replace the on event with live then it starts working. Code

$(".draggable").live("mouseover", function () {
                if (!$(this).data("init")) {
                    $(this).data("init", true).draggable();
                }
});

can anybody explain me what i am doing wrong ?

Upvotes: 2

Views: 49

Answers (2)

I Hate Lazy
I Hate Lazy

Reputation: 48789

If .live() works, then that likely means that the element you're manipulating is not present when the code runs.

This either means you are not waiting for the DOM to be ready before running the code, or you need to use the delegated version of .on instead.

$(function() {
    $(document).on("mouseover", ".draggable", function () {
                    if (!$(this).data("init")) {
                        $(this).data("init", true).draggable();
                    }
    });
});

You should replace document with a selector for the deepest nested container that contains all the .draggable elements.

Upvotes: 3

Pragnesh Chauhan
Pragnesh Chauhan

Reputation: 8476

it depend on which jquery version you use

$(selector).live(events, data, handler);                // jQuery 1.3+
$(document).delegate(selector, events, data, handler);  // jQuery 1.4.3+
$(document).on(events, selector, data, handler);        // jQuery 1.7+  

refer this link for more details

Upvotes: 2

Related Questions