javagwtgwtquery

Selecting DIV or Anchor with specific attribute with GwtQuery


With GwtQuery how do I select all anchor elements with a specific attribute and attach click handler for each element found:

    <ul data-field="navDropdown" class="uk-nav uk-nav-dropdown">
        <li><a custom="none">None</a></li>
    </ul>

Here is my code that does not work:

    $("a[attr=custom]").each(new Function() { // I've also tried 'att'
        @Override
        public void f(final com.google.gwt.dom.client.Element e) {
            final AnchorElement anchorElement = e.cast();
            Anchor a = Anchor.wrap(anchorElement);
            a.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent clickEvent) {
                    Window.alert(anchorElement.getInnerText());
                }
            });
        }
    });

Solution

  • The css attribute selector is [attribute_name=attribute_value] or [attribute_name] to select all elements with an attribute named attribute_name : https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

    so change you code like this:

    $("a[custom]").each(...)
    

    or

    $("a[custom=none]")