Can somebody tell me how to focus on particular part of a web page by using JavaScript events? Let's say I clicked a button and it will scroll down or scroll up to a particular portion a page. Here is my inplete code:
<html>
<head>
<script type="text/javascript">
function focusOnForm() {
document.getElementById('form').style.display = '';
}
</script>
</head>
<body onload="document.getElementById('form').style.display = 'none';">
<div style="height: 900px; width: width: 500px; background-color: #000;">
</div>
<br />
<input type="button" value="Add" style="float: right;" onclick="focusOnForm();" />
<br />
<div id="form">
First Name: <input type="text" /> <br />
Last Name: <input type="text" /> <br />
Address: <input type="text" /> <br />
Contact Number: <input type="text" /> <br />
Email: <input type="text" /> <br />
</div>
</body>
</html>
I really need help.
Can somebody tell me how to focus on particular part of a web page by using JavaScript events? Let's say I clicked a button and it will scroll down or scroll up to a particular portion a page. Here is my inplete code:
<html>
<head>
<script type="text/javascript">
function focusOnForm() {
document.getElementById('form').style.display = '';
}
</script>
</head>
<body onload="document.getElementById('form').style.display = 'none';">
<div style="height: 900px; width: width: 500px; background-color: #000;">
</div>
<br />
<input type="button" value="Add" style="float: right;" onclick="focusOnForm();" />
<br />
<div id="form">
First Name: <input type="text" /> <br />
Last Name: <input type="text" /> <br />
Address: <input type="text" /> <br />
Contact Number: <input type="text" /> <br />
Email: <input type="text" /> <br />
</div>
</body>
</html>
I really need help.
Share Improve this question edited Sep 2, 2022 at 15:46 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 5, 2011 at 18:36 NinjaBoyNinjaBoy 3,75519 gold badges57 silver badges69 bronze badges2 Answers
Reset to default 3Use the .scrollIntoView()
method:
function focusOnForm() {
var form = document.getElementById('form');
form.style.display = '';
form.scrollIntoView();
}
Fiddle: http://jsfiddle/g8Mqr/
You can scroll to a pixel location with:
document.body.scrollTop = 2000;
http://jsfiddle/ThinkingStiff/A7JMZ/