INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Add autofocus dynamically

Add autofocus dynamically

(OP)
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

RE: Add autofocus dynamically

CODE

$("#SecurityAnswer]").attr("autofocus","autofocus").focus(); 

RE: Add autofocus dynamically

(OP)
That will work.

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

Thanks,

Tom

RE: Add autofocus dynamically

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).

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close