aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes set focus using js file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "set focus using js file" Watch "set focus using js file" New topic
Author

set focus using js file

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Hi ,
i have 2 text box in my form (FirstName, and LastName) . When the value of one text box changes i call genericValidation on onchange event.



In the above code which is in js file, how i can set the focus to LastName.

thnx is advance


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Pavan Keely
Ranch Hand

Joined: Jun 30, 2006
Posts: 62
N Chaurasia,

Use focus() on textbox object, like

document.<formname>.<lastname textboxname>.focus(). If you have "id' defined for lastname textbox, then you can also focus that element like,

document.getElementById(" <lastname textbox id>").focus();


Pavan Keely<br /><a href="http://keelypavan.blogspot.com" target="_blank" rel="nofollow">http://keelypavan.blogspot.com</a>
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
pavan i tried it but it is not working
Wayan Suwastika
Greenhorn

Joined: May 25, 2005
Posts: 11
To set an object properties for common using you may pass an object reference. May be you should change the argument of your js function like this :

argObj --> the text field object ie. FirstName/LastName
objValue --> some value entered on that field

function genericValidation(argObj, objValue)
{

//set focus
argObj.focus();
return true;
}

your HTML code may look like this :

<form name="someForm">
<input type="text" name="FirstName" value="" onchange="genericValidation(document.someForm.LastName, this.value)">
<input type="text" name="LastName" value="">
</form>

Correct me if i'm wrong. thanks.

Rgds,
Wyasa


SCJA, SCJP 1.4, SCWCD inprogress
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Wayan , i tried above code...it does not help
Pavan Keely
Ranch Hand

Joined: Jun 30, 2006
Posts: 62
N Chaurasia,

Can you please post the code that you are trying to use with the HTML used for last name and firstname text boxes.
Roy Ivar Moe
Greenhorn

Joined: Jun 22, 2006
Posts: 21
Hey,

I suspect you are using IE( = BUGGY ).

First of all, for more ensured selection, you need to invoke both the
focus() and select() methods for your text field. Second, you need to create an artifical delay before trying to set focus to your control.

Try something like this:



You need to pass the element id instead of a reference to the element in the function above, but you could rewrite it to take an object reference if you wanted to.

Hope this helps,
Regards,
Roy

[ July 11, 2006: Message edited by: Roy Ivar Moe ]
[ July 11, 2006: Message edited by: Roy Ivar Moe ]
Wayan Suwastika
Greenhorn

Joined: May 25, 2005
Posts: 11
I try to figure out your problem. It seem the onChange event will only fired when an object is lost focus or onLostFocus or onBlur event was fired.
May be you sould change the way you move the focus with call the
onKeyUp event on your Text field object. This event will call when a keyboad key is up (release) and its mean the content was of your text field was changed.
The HTML code may look like this:


pls replace space at o n K e y U p word.

Rgds,
Wyasa
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: set focus using js file