Problem with focus

Discussion in 'JavaScript' started by adamjblakey, Jun 19, 2007.

  1. #1
    Hi,

    I have a function which hides and shows a div.

    When i click the link to show the the div the page loads at the top. What i want to do is keep the focus on the link.

    This is what i am using but does not seem to work?

    function change(which) {
       document.getElementById('div1').style.display = 'block';
       document.getElementById('div1').focus();
       }
    Code (markup):
    Here is the link:
    <a href="#" onclick="change('div1');">Click here</a>
    Code (markup):
    Any ideas why it is not working?
     
    adamjblakey, Jun 19, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    <a href="#" onclick="change('div1'); return false;">Click here</a>
    
    HTML:
    Just add a return false at the onclick attribute.

    Or replace the # with javascript:;
    
    <a href="javascript:;" onclick="change('div1');">Click here</a>
    
    HTML:
    I think void(0); should work too...
     
    nico_swd, Jun 19, 2007 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Cheers nico your a star as usual ;)
     
    adamjblakey, Jun 19, 2007 IP