views:

12187

answers:

5

Hi,

I have done the following code in JavaScript to put focus on the particular element (branch1 is a element),

document.location.href="#branch1";

But as I am also using jQuery in my web app, so I want to do the above code in jQuery. I have tried but don't know why its not working,

$("#branch1").focus();

The above jquery (focus()) code is not working for div, whereas If i am trying the same code with textbox, then its working,

Please tell me, how can I put focus on a div elemnt using jQuery?

Thanks!

+3  A: 

Duplicate of this question.

davogones
Yes, same. Thanks! I will try, jquery scrollTo plugin and if solved will let you know. Thanks again.
Prashant
@davogones: Comments should be used for flagging duplicates.
Casebash
+1  A: 

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.

Luca Matteis
Ya I'm trying to achieve the same anchoring task. Ok, if the focus can't do it, is there any other method in jquery which can do this?
Prashant
+9  A: 

Check jQuery.ScrollTo, I think that's the behavior that you want, check the demo.

CMS
Ya, I am checking it. Thanks
Prashant
+1  A: 

I realize this is old but i just ran across it and no one had posted an answer. At least if i understand correctly anyway. Anyway for my problem this code worked i need to navigate to an anchor tag on page load.

$(this).scrollTop($('a#captchaAnchor').position().top)

for that matter you can use this on any element not just an anchor tag

Banning
A: 

Like @user293153 I only just discovered this question and it didn't seem to be answered correctly.

His answer was best. But you can also animate to the element as well.

$('html, body').animate({ scrollTop: $("#some_element").offset().top }, 500);
Josh Stuart