-3

I have made a site with multiple pages in it. These pages are in linked by a menu. One button of the menu takes the user to another page but to a specific spot, a specific div by it`s id. The problem is that when I click the button it first takes you to the top of the page and after that spot. Can I avoid this?

EDIT:

$evenimente='#evenimente';

    return
        '<div class="header">'.
            '<a class="prev" href="'.$this->naviHref.'?month='.sprintf('%02d',$preMonth).'&year='.$preYear.$evenimente.'">Prev</a>'.
                '<span class="title">'.date('Y M',strtotime($this->currentYear.'-'.$this->currentMonth.'-1')).'</span>'.
            '<a class="next" href="'.$this->naviHref.'?month='.sprintf("%02d", $nextMonth).'&year='.$nextYear.$evenimente.'">Next</a>'.
        '</div>';

It`s actually a button which changes the month in a calendar, and if I press it it takes me on the top of the page again

Don
  • 3,876
  • 10
  • 47
  • 76
  • You're saying you have to click the button a second time to go to the spot on the page?... – War10ck May 19 '17 at 20:31
  • Please post what you have tried so far. – marcelovca90 May 19 '17 at 20:32
  • No, it s just a visual problem. It shows very fast the top and after what I want. I have to use it in the page where the spot is, and it s very annoying – Sergiu Tritean May 19 '17 at 20:33
  • Sounds like a browser behavior that you won't be able to avoid. – War10ck May 19 '17 at 20:33
  • You need to use `e.preventDefault()` as what it is described on this [link](http://stackoverflow.com/questions/3252730/how-to-prevent-a-click-on-a-link-from-jumping-to-top-of-page-in-jquery). Possible duplicate. – threeFatCat May 19 '17 at 21:32
  • Possible duplicate of [How to prevent a click on a '#' link from jumping to top of page in jQuery](http://stackoverflow.com/questions/3252730/how-to-prevent-a-click-on-a-link-from-jumping-to-top-of-page-in-jquery) – threeFatCat May 20 '17 at 05:01
  • The point is that i`m using PHP, not jQuery, and I don`t know how to do what i`ve could do in jQuery in PHP – Sergiu Tritean May 20 '17 at 11:41
  • Php is a server side language. That one has nothing to do with PHP. You need to use `jQuery` or Javascript to prevent it from jumping. You cant develop web in your lifetime using `php` alone. – threeFatCat May 21 '17 at 15:15

1 Answers1

0

You need jQuery for that and using e.preventDefault(); as what it is described on this answer: How to prevent a click on a '#' link from jumping to top of page in jQuery

$('a.someclass').click(function(e)
{
    // Special stuff to do when this link is clicked...

    // Cancel the default action
    e.preventDefault();
});
Community
  • 1
  • 1
threeFatCat
  • 840
  • 13
  • 30