Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

focus a DIV 2

Status
Not open for further replies.

Kendel

Programmer
Apr 24, 2002
1,512
US
Hi,

How do you set focus on a div? I used document.getElementById("name").focus(); but nothing happen; the DIV didn't get focused.

Thanks.
 
I am just curious, but why do you want to set focus to a div element?

Anyways, on a whim, I tried the code below; it appeared to work:

Code:
<div [b]tabindex="0"[/b] id="benluc">Here you go</div>

<script>
document.getElementById("benluc").focus();
</script>

The key is the addition of tabindex="0" to the DIV element's properties.

Thanks,

jared@eae.net -
 
Kendel,

if what you're after is to move a page up or down so that something off-screen gets moved to the top, I would put an anchor next to your DIV tag. For example, put this at the top of the page:

Code:
<a href="#" name="TOP_FOCUS"></a><div ...> ... </div>

Then, wherever you want it, you can push the page to here with a

Code:
TOP_FOCUS.focus();

I didn't know about jaredn's solution. I knew I was never able to throw focus to a DIV, but I didn't know that I COULD have if I had a tabindex in there. Neat!

--Dave
 
btw Dave,

The href='#' is not needed when making an anchor on your page to jump to. This is sufficient:

<a name=blah></a>

-kaht

banghead.gif
 
ohhhh ...the tapindex="0" key word! Thanks jaredn!

LookingForInfo, I can use your "top_focus" to get back to top when I'm done viewing one section. Thanks.
 
There's also a scrollIntoView() method that you could use.

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top