Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add autofocus dynamically 1

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
0
0
US
I have two input fields. Only one will be visible on the screen at once.

I control which one is visible with variable (Model.Step). If Model.Step = "0", the UserId field shows. If Model.Step = 2, the SecurityAnswer shows and the UserId is hidden.

UserID, already has the autofocus attribute on the input element.

When I press the submit the page will redisplay with the UserId hidden and the SecurityAnswer visible. But the SecurityAnswer does not have focus.

I tried the following:

Code:
if (currentStep == "2")
{
	$("#UserId").removeAttr("autofocus");
	$("#SecurityAnswer]").attr("autofocus","autofocus");
}

If I F12 the screen the UserId now does not have the autofocus attribute and the SecurityAnswer does. But the SecurityAnswer still does not have focus.

I assume this is because the IE has already determined what has focus before the attribute gets changed.

Is there a way to make this work with JQuery?

Thanks,

Tom
 
Code:
$("#SecurityAnswer]").attr("autofocus","autofocus")[red].focus()[/red];
 
That will work.

But I don't need to add the autofocus attribute if I use .focus().

Thanks,

Tom
 
your question is not clear.

are you asking how to change the autofocus attribute or how to focus on a field?

changing the autofocus attribute is done as i have posted. however the browser looks at this only on load; so it is largely pointless unless you are using the attribute for another co-purpose (qua selector for example).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top