Home » Javascript

JavaScript - Set Focus on Particular Field in a Form using JavaScript Function

In this example, we will learn how to set focus in any textbox (field) in a form using JavaScript Function.

JavaScript Code Snippet – Set Focus in any Field of a Form using JavaScript Function

JavaScript function

<script type="text/javascript">
	function setFocus(indexForm, indexField){
		document.forms[indexForm].elements[indexField].focus();
	}
</script>

HTML Source Code with JavaScript

<!--JavaScript - Set Focus on Particular Field in a Form using JavaScript Function.-->
<html>
	<head>
		<title>JavaScript - Set Focus on Particular Field in a Form using JavaScript Function.</title>
		<script type="text/javascript">
			function setFocus(indexForm, indexField){
				document.forms[indexForm].elements[indexField].focus();
			}
		</script>
	</head>

	<body onload='setFocus(0,0)'>
		<h1>JavaScript - Set Focus on Particular Field in a Form using JavaScript Function.</h1>
		<form>
			<b>Name:</b><br/>
			<input type="text" width="100px"/><br/>
			<b>Address:</b><br/>
			<input type="text" width="200px"/><br/>
			<b>Mobile:</b><br/>
			<input type="text" width="50px"/><br/>
			<input type="button" value="Submit."/>
		</form>
	</body>

</html>

Result

set text on status bar using javascript function





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.