I am trying to focus the username input when a button it pressed. I have tried autofocus but that doesn't seem to work. Any suggestions?
I don't really want to use javascript for this (haven't yet!) but I know I might have to.
<a class="button" href="#popup1" accesskey="r"><img class="access" src="images/login.png" /></a>
<div id="popup1" class="overlay">
<div class="popup">
<div class="popup_header">
<h2>Log In</h2>
<a class="close" href="#" accesskey="w">×</a>
</div>
<div class="content">
<section>
<ul class="input-list cred clearfix">
<form action="login.php" method="POST">
<li>
<input type="text" name="username" placeholder="Username">
</li>
<li>
<input type="password" name="password" placeholder="Password">
</li>
<li>
<input type="submit" name="submit" value="Sign In">
</li>
</ul>
</section>
</div>
</div>
According to your markup for popup I could suggest this method for showing the popup and focusing the input (in this case only the first type text input):
$('a').click(function(e) {
e.preventDefault();
var target = $(this).attr('href');
$(target).show();
$($('input[type=text]', target).get(0)).focus();
})
autofocus works in IE10+, Chrome, FireFox - please test in other browsers
but it works only on page load.
<input type="text" name="username" placeholder="Username" autofocus/>
Non supported browsers require JavaScript/Jquery to provide a focus on page
load.
focus on button click always require JavaScript/Jquery.
Try this.
$("#buttonid").click(function(){
//put an id to your username inputbox
$("#username").focus();
});
You cannot do this without javascript
, you can write simple inline javascript.
see the below example:
a:focus, a:active { color: green; }
<form name="myform2"> <input type="text" name="mytextfield2"> <input type="button" name="mybutton" value="Set Focus" OnClick="document.myform2.mytextfield2.focus();"> </form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.