autofocus

In this example below you will see how to do a autofocus with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by iamdavidobrien, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright iamdavidobrien ©

Technologies

  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>autofocus</title>
  
  
  
  
  
</head>

<body>

  
	<head>
		<title>My form demo</title>
	    <link rel="stylesheet" href="https://resources.mygov.scot/assets/css/bootstrap.min.css">
	</head>

	<body>
		<h1>Heading level one</h1>
		<nav class="menu">
		  <ul>
		    <li><a href="#">Home</a></li>
		    <li><a href="#">About</a></li>
		    <li><a href="#">Contact</a></li>
		  </ul>
		</nav>
		<main>
			<form action="#">
				<fieldset class="email">
					<label for="email">Enter your email address</label>
					<input id="email" type="email" autofocus>
					<button class="go">Go</button>
				</fieldset>
				<fieldset class="password">
					<label>Password
					<input id="password" type="password" aria-labelledby="pwhelp" placeholder="Type your password">
					</label>
					<p id="pwhelp">Welcome, David. You already have an account with us. Enter your password to continue.</p>
					<button class="signin">Sign in</button>
				</fieldset>
			</form>
		</main>
		<footer>
		</footer>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

	</body>
  <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/iamdavidobrien/autofocus-NvwxbZ */
			$( document ).ready(function() {
				$('.password').hide();
				$('.go').click(function(){
					$('#email').removeAttr('autofocus')
					$('.email').hide();
					$('.password').show();
					$('#password').attr('autofocus', true);

				});
			});

Comments