Link to home
Start Free TrialLog in
Avatar of Gary Croxford
Gary CroxfordFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Javascript setfocus() function not consistent

I have a php page and am trying to set the focus to a specific text input box when the page opens using this mechanism:

<script type="text/javascript" language="JavaScript">
	function jsSetFocus(){
		document.forms["OrderDetail"]["ProdnOrder"].disabled = true;
		document.forms["OrderDetail"]["VECode"].focus();
	}
	jsSetFocus();

Open in new window


my problem is that the focus event only fires intermittently - the ProdnOrder input is disabled but the focus is on the path in the address box which is highlighted. but occasionally the focus event works as it should.

How can I force the focus onto the VECode input box everytime?
Avatar of Swatantra Bhargava
Swatantra Bhargava
Flag of India image

Made the below change and check result

function jsSetFocus()
{
		document.forms["OrderDetail"]["ProdnOrder"].disabled = true;
		[b]$( "#VECode" ).focus(); // VECode -- ID of your control[/b]

OR

document.getElementById("VECode").focus();

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gary Croxford

ASKER

>>Julian Hansen

Works a treat, thank you.

I had tried disabled and autofocus attributes for the relevant elements but they aren't working, don't know why. Would the fact that I'm writing the html as echo commands - echo '<input type="text" ... etc have any impact? Can't think why it would.
No it should not have an impact that the code is being generated dynamically.

Do you have a sample of the rendered output that is not working (where disabled and autofocus are defined as part of the element)?