ArcBees / gwtquery

A jQuery clone for GWT, and much more.
MIT License
85 stars 38 forks source link

$(this).index() always returns 0 in callback #330

Open confile opened 9 years ago

confile commented 9 years ago

I use the following code:

$(contentList).find(".mytouch")
        .on("click", new Function() {

            public boolean f(Event ev) {

              int index = $(this).index();
              GWT.log("div index: "+index);

              return true;
            }
        });

$(this).index() always returns 0 in click callback

confile commented 9 years ago

It is also strange that the following works:

$(contentList).find(".mytouch")
        .on("tap", new Function() {

            public boolean f(Event ev) {

              GWT.log("div tapped");

              return true;
            }
        });

and the following does NOT work:

$(contentList).on("tap", ".mytouch", new Function() {

            public boolean f(Event ev) {

              GWT.log("div tapped");

              return true;
            }
        });
jDramaix commented 9 years ago

$(this).index() always returns 0 in click callback

Why do you expect to have ? The javadoc of the index() method syas "Return the position of the first matched element in relation with its sibblings."

For the second problem, please open a new issue otherwise it will be a mess to discuss about two different issues on the same thread

confile commented 9 years ago

@jDramaix How do I get the selected elements index?

manolo commented 9 years ago

I have not tested but for the first case I think this code should work as you expect:

   $(contentList).find(".mytouch")
        .on("click", new Function() {
            public boolean f(Event ev) {
              int index = $(contentList).index($(this).get(0));
              GWT.log("div index: "+index);
              return true;
            }
        });

Or in a simplified version:

   $(".mytouch", contentList).on("click", new Function() {
            public void f(Element e) {
              int index = $(contentList).index(e);
              GWT.log("div index: "+index);
            }
        });

The second case seems a different issue since you are using live, so please check if live works with the code above, and using native events like click. If it works, try with the 'tap' event of the gestures plugin and report to the plugin in the case it's not a gquery problem

confile commented 9 years ago

I checked both your suggestions and I always get index -1. There is another problem. the on() function does not work like the JQuery on function. When I do

$(".mytouch", contentList).on("click", new Function() { ... }

It should attach a handler to all the elements of contentList with class mytouch. This does not happen with GWTQuery on(). The handlers are only attached to elements with class mytouch which are appended to contentList at the time of executing this command.