0

I have a page with lots of contents, I want to put a hyperlink in the top, that scrolls the page to a div . this is my code :

        <a id = "show" class="show" href="#" onclick="return toggleOptions(this);"><h2>FIRST BUTTON</h2></a>
        <div id="toggleOptions" class="toggleOptions"> // this is div which is somewhere in center of page
        text text text....
        more text text.... 
        text text.........
        </div>

I know that I need to use JavaScript, something like that:

<script type='text/javascript'>
    window.location = "/index.php#myDiv";
</script>

Just don't know how to do It correctly. Please help me .

Igor Ševo
  • 5,459
  • 3
  • 35
  • 80

4 Answers4

1

No need to javascript, simply do this :

   <a id = "show" class="show" href="#toggleOptions"><h2>FIRST BUTTON</h2></a>
    <div id="toggleOptions" class="toggleOptions"> 
    text text text..
    more text text.. 
    text text.......
    </div>
user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
1

Try:

<a href="#idOfDiv">Go to my div</a>

<div id="idOfDiv">...</div>
Streamside
  • 332
  • 1
  • 8
1
<a id = "show" class="show" href="#toggleOptions"><h2>BUTTON</h2></a>
    <div id="toggleOptions" class="toggleOptions"> 
    text    
    </div>
user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
1

No need of having javascript for this. Just use the href of tag as follows:

    <a href="#toggleOptions">

Where #toogleOptions is the id of your target divs.

Abu Isaac
  • 75
  • 2
  • 13