gwtquery icon indicating copy to clipboard operation
gwtquery copied to clipboard

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

Open confile opened this issue 10 years ago • 5 comments

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 avatar Jan 02 '15 15:01 confile

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;
            }
        });

confile avatar Jan 02 '15 15:01 confile

$(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

jDramaix avatar Jan 02 '15 15:01 jDramaix

@jDramaix How do I get the selected elements index?

confile avatar Jan 02 '15 15:01 confile

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

manolo avatar Jan 02 '15 16:01 manolo

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.

confile avatar Jan 03 '15 20:01 confile