I'm writing an autocomplete component for a webapp using Java and Wicket.
Is there a way to handle the onchange event to run some code when the user selects one of the options of the autocomplete list? I tried doing this in the AutoCompleteTextField:
        setOutputMarkupId(true);
        add(new AjaxEventBehavior("onchange") {
            @Override
            protected void onEvent(AjaxRequestTarget target) {
                System.out.println(getInput());
            }
        });
But the getInput method returns null. :(
Is there a way to react to the onchange event and to be able to read what the user has entered?
Thanks for you time and knowledge :)
The onchange event is only fired when the focus is moved away from the component. (This is a universal browser/javascript thing.)
You need to hook your handler to the onkeypress event instead.
What you need is not AjaxEventBehavior but AjaxFormComponentUpdatingBehavior:
    add( new AjaxFormComponentUpdatingBehavior( "onchange") {
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            System.out.println( "Value: "+field.getValue() );
        }
    });
Although it works with getInput() too, but usually the somewhat higher level (properly escaped and backed by the model) getValue() is a better fit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With