Saturday, January 21, 2012

jQuery: how to set focus in page load to any element in page

Here are 2 simple steps...to make it.
-----------------------------------------------------
Step:1  : Identify the element, you want to set focus
-----------------------------------------------------
<body>
<div id="mainwrapper">
<!-- container1 -->
<div id="container1" 
style="height:300px;width:500px;display:block;">
Lorem Ipsum is simply dummy text 
</div>

<!-- container2 -->
<div id="container2 
style="height:300px;width:500px;display:block;">
Lorem Ipsum is simply dummy text 
</div>

</div>
</body>

In the above html code, 
suppose we need focus to "container2".

-----------------------------------------------------
Step:2 : place this jquery snippet in ..
-----------------------------------------------------
<script type="text/javascript">
$(document).ready(function() {
   $('#container2').get(0).scrollIntoView();  
});
</script>



That's it... :-)