I have done the following code in JavaScript to put focus on the particular element (branch1 is a element),
document.location.href=\"#branch1\";
If you're simply trying to scroll to the specified element, you can use the scrollIntoView method of the Element. Here's an example :
$target.get(0).scrollIntoView();
I think you might be looking for an "anchor" given the example you have.
<a href="#jump">This link will jump to the anchor named jump</a>
<a name="jump">This is where the link will jump to</a>
The focus jQuery method does something different from what you're trying to achieve.
Check out jquery-scrollintoview.
ScrollTo is fine, but oftentimes you just want to make sure a UI element is visible, not necessarily at the top. ScrollTo doesn't help you with this. From scrollintoview's README:
How does this plugin solve the user experience issue
This plugin scrolls a particular element into view similar to browser built-in functionality (DOM's scrollIntoView() function), but works differently (and arguably more user friendly):
- it only scrolls to element when element is actually out of view; if element is in view (anywhere in visible document area), no scrolling will be performed;
- it scrolls using animation effects; when scrolling is performed users know exactly they're not redirected anywhere, but actually see that they're simply moved somewhere else within the same page (as well as in which direction they moved);
- there's always the smallest amount of scrolling being applied; when element is above the visible document area it will be scrolled to the top of visible area; when element is below the visible are it will be scrolled to the bottom of visible area; this is the most consistent way of scrolling - when scrolling would always be to top it sometimes couldn't scroll an element to top when it was close to the bottom of scrollable container (thus scrolling would be unpredictable);
- when element's size exceeds the size of visible document area its top-left corner is the one that will be scrolled to;