I want to set focus on particular div using jquery or javascript. i have just use document.getElementById("pack_0").focus();
in that 'pack_0' is my div id but it doesn't work. I notice that '#' is already added in my url because of some popup coding, so my url is 'http://localhost/website/book#'
So, can any one help me for force fully focus on div when # in url?
Thanks in advance.
The focus() is an inbuilt method in jQuery which is used to focus on an element. The element get focused by the mouse click or by the tab-navigating button. Here selector is the selected element. Parameter: It accepts an optional parameter “function” which specifies the function to run when the focus event occurs.
Yes - this is possible. In order to do it, you need to assign a tabindex... A tabindex of 0 will put the tag "in the natural tab order of the page".
From the question, I think what you actually mean by "focus the div" is "change the window's vertical scroll position so the div is visible". If that is the case, you can use the following bit of jQuery code:
$(window).scrollTop($('#pack_0').offset().top);
Anthony Grist's assumption is likely correct in your case. However, it may help others searching on this topic to know that if the desired object of focus is a DIV, that DIV should have a tabindex set, e.g., tabindex="9999". Otherwise, most browsers will not assign the focus.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With