You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Doug Leeper <do...@yahoo.com> on 2007/09/21 00:43:45 UTC

Setting focus to a TextField

I know this is probably one of the most trivial things to do...but I am
stumped.  No example that I found on the Wicket example site has shown this. 
I know that AjaxRequestTarget#focusComponent plays a part in this but not
sure how.  Could someone post a quick code snippet or place on the Wiki on
how do to this in 1.3?

Thanks
-- 
View this message in context: http://www.nabble.com/Setting-focus-to-a-TextField-tf4490766.html#a12807454
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Setting focus to a TextField

Posted by Eelco Hillenius <ee...@gmail.com>.
> I would like to use the code snippet you're providing, but I don't fully
> understand it... Why are you creating an AjaxLink and then setting the focus
> on the TextField within the onClick() method of the AjaxLink?

I think it was just an example. The question was how to set focus on a
text field after handling an ajax request right? An AjaxLink is one
example of such an ajax request.

Eelco

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Setting focus to a TextField

Posted by Cristina <cr...@acm.org>.
Hi Kent,

I would like to use the code snippet you're providing, but I don't fully
understand it... Why are you creating an AjaxLink and then setting the focus
on the TextField within the onClick() method of the AjaxLink?

Thanks so much,

Cristina



Kent Tong wrote:
> 
> 
> 
> Doug Leeper wrote:
>> 
>> I know this is probably one of the most trivial things to do...but I am
>> stumped.  No example that I found on the Wicket example site has shown
>> this.  I know that AjaxRequestTarget#focusComponent plays a part in this
>> but not sure how.  Could someone post a quick code snippet or place on
>> the Wiki on how do to this in 1.3?
>> Thanks
>> 
> 
> For example, your TextField is f1, then:
> 
> <pre>
> 		AjaxLink link = new AjaxLink("link") {
> 			public void onClick(AjaxRequestTarget target) {
> 				target.addComponent(...);
> 				target.focusComponent(f1);
> 			}
> 		};
> </pre>
> 

-- 
View this message in context: http://www.nabble.com/Setting-focus-to-a-TextField-tf4490766.html#a12948392
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Setting focus to a TextField

Posted by Kent Tong <ke...@cpttm.org.mo>.


Doug Leeper wrote:
> 
> I know this is probably one of the most trivial things to do...but I am
> stumped.  No example that I found on the Wicket example site has shown
> this.  I know that AjaxRequestTarget#focusComponent plays a part in this
> but not sure how.  Could someone post a quick code snippet or place on the
> Wiki on how do to this in 1.3?
> Thanks
> 

For example, your TextField is f1, then:


		AjaxLink link = new AjaxLink("link") {
			public void onClick(AjaxRequestTarget target) {
				target.addComponent(...);
				target.focusComponent(f1);
			}
		};

-- 
View this message in context: http://www.nabble.com/Setting-focus-to-a-TextField-tf4490766.html#a12814170
Sent from the Wicket - User mailing list archive at Nabble.com.

Re: Setting focus to a TextField

Posted by Federico Fanton <ff...@ibc.it>.
On Thu, 20 Sep 2007 20:21:20 -0400
John Krasnay <jo...@krasnay.ca> wrote:

> I include this little script in my base page to always auto-focus the
> first text box.

Pretty! Thanks :)


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Setting focus to a TextField

Posted by John Krasnay <jo...@krasnay.ca>.
On Thu, Sep 20, 2007 at 03:43:45PM -0700, Doug Leeper wrote:
> 
> I know this is probably one of the most trivial things to do...but I am
> stumped.  No example that I found on the Wicket example site has shown this. 
> I know that AjaxRequestTarget#focusComponent plays a part in this but not
> sure how.  Could someone post a quick code snippet or place on the Wiki on
> how do to this in 1.3?
> 
> Thanks

I include this little script in my base page to always auto-focus the
first text box.

Focus = {};

Focus.setInitialFocus = function() {

    // look for inputs with CSS class "invalid" first
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        var input = inputs.item(i);
        if (input.className.indexOf("invalid") >= 0) {
            input.focus();
            input.select();
            return;
        }
    }

    for (var i = 0; i < inputs.length; i++) {
        var input = inputs.item(i);
        if (input.type == "text"
            || input.type == "file"
            || input.type == "password"
            || input.type == "radio"
            || input.type == "checkbox") {

            // skip it if it's disabled or has the nofocus CSS class
            if (!input.disabled && input.className.indexOf("nofocus") ==
-1) {
                input.focus();
                return;
            }
        }
    }
}

if (window.addEventListener) {
    window.addEventListener("load", Focus.setInitialFocus, true);
} else {
    if (window.attachEvent) {
        window.attachEvent("onload", Focus.setInitialFocus);
    }
}

jk

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Setting focus to a TextField

Posted by Raluca Macovei <ra...@gmail.com>.
If you need to change focus from the server side:
textField.add(new AjaxFormComponentUpdatingBehavior("onblur") {

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				target.addComponent(MyPanel.this);				
				String scriptStart = "document.getElementById('";
				String scriptEnd = "').focus()";
				if (textField.hasErrorMessage()) {
					target.appendJavascript(scriptStart + textField.getMarkupId() + scriptEnd);
				} else if (aCondition) {
					target.appendJavascript(scriptStart
							+ firstPanel.getFirstFocusableComponent().getMarkupId() + scriptEnd);
				} else {
					target.appendJavascript(scriptStart
							+ secondPanel.getFirstFocusableComponent().getMarkupId() + scriptEnd);
				}
			}
		});

/Raluca
On 9/21/07, Gwyn Evans <gw...@gmail.com> wrote:
> On Thursday, September 20, 2007, 11:43:45 PM, Doug <do...@yahoo.com> wrote:
>
> > I know this is probably one of the most trivial things to do...but I am
> > stumped.  No example that I found on the Wicket example site has shown this.
> > I know that AjaxRequestTarget#focusComponent plays a part in this but not
> > sure how.  Could someone post a quick code snippet or place on the Wiki on
> > how do to this in 1.3?
>
> Well, the basic code you need is just a
>       <script language="JavaScript">
>             document.myform.myinput.focus();
>       </script>
> in the markup (if you use <body onLoad...> you may find it being
> executed before that page if fully loaded) but I've not tried using
> Wicket to tweak it.
>
> /Gwyn
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Setting focus to a TextField

Posted by Gwyn Evans <gw...@gmail.com>.
On Thursday, September 20, 2007, 11:43:45 PM, Doug <do...@yahoo.com> wrote:

> I know this is probably one of the most trivial things to do...but I am
> stumped.  No example that I found on the Wicket example site has shown this.
> I know that AjaxRequestTarget#focusComponent plays a part in this but not
> sure how.  Could someone post a quick code snippet or place on the Wiki on
> how do to this in 1.3?

Well, the basic code you need is just a
      <script language="JavaScript">
            document.myform.myinput.focus();
      </script>
in the markup (if you use <body onLoad...> you may find it being
executed before that page if fully loaded) but I've not tried using
Wicket to tweak it.

/Gwyn


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org