Setting focus of page to a Div

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bananna
    New Member
    • Mar 2008
    • 2

    Setting focus of page to a Div

    Hello,
    In my site I have a link and you click on it, it runs a javascript function where it gets makes a div visable. This is so people can click the link and get a defintion of a word.
    Here is the link:

    [HTML]<a href="#" onclick="toggle _visibility('pr ocessIslands'); ">(What are Process Islands?)</a>
    [/HTML]

    Here is the Div:
    [HTML] <div id="processIsla nds" style="display: none"> Text in the Div </div>
    [/HTML]
    And here is the function that is called:
    [CODE=javascript] function toggle_visibili ty(id) {
    var div = document.getEle mentById(id);
    if(div.style.di splay == 'block')
    div.style.displ ay = 'none';
    else
    div.style.displ ay = 'block';

    }
    [/CODE]
    My problem, is that when I use this as it is, it will show the div, but then it jumps to the top of the page. How can I set the focus of the page to that Div so the user doesn't have to scroll down and find where they were?

    I am using asp.net C# Visual Studio 2005.

    Thanks!
    Last edited by acoder; Mar 13 '08, 04:15 PM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Just add return false to prevent the link being followed to #:
    [HTML]<a href="#" onclick="toggle _visibility('pr ocessIslands'); return false;">(What are Process Islands?)</a>
    [/HTML]

    Comment

    • bananna
      New Member
      • Mar 2008
      • 2

      #3
      Thanks for that, It helped with that problem, but I sill was wondering how to set the focus to a div when a link is clicked. The scenario is a glossary, and when the user clicks on a word, they are focused on that words definition on the page. How would I go about doing that?

      Thanks!

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Use <a name="#somethin g"> next to the div to link to it, then use that in the href of the link and remove the "return false" in the onclick:[HTML]<a href="#somethin g" onclick="toggle _visibility('pr ocessIslands'); ">(What are Process Islands?)</a>
        [/HTML]Replace "something" with something meaningful.

        Comment

        Working...