Rajesh Rs
Rajesh Rs

Reputation: 1391

How to navigate to the HTML element in the same page using javascript...

I want to navigate to an HTML element having a particular 'id' in the same page using javascript on click of a button.

for example:

<body>
   <div id="navigateHere" >
   </div>

   <button onclicK="navigate();" />

In the above code what should be there in the javascript function navigate() , so that on a click of a button , it will navigate to the 'div' element with an id 'navigateHere'.....

Thanks in advance ...

Upvotes: 7

Views: 17832

Answers (3)

Kevin Ji
Kevin Ji

Reputation: 10489

Instead of a button, you can use a simple link:

<div><a href="#navigateHere">Link text</a></div>

If you need to use JavaScript and a button, something like the following should work:

HTML:

<button type="button" id="someid">Link text</button>

JavaScript:

document.getElementById("someid").onclick = function () {
    window.location.hash = "#navigateHere";
};

Upvotes: 10

alex
alex

Reputation: 490153

It will navigate to the div element...

This may be what you want.

window.location.hash = 'navigateHere';

There is no need to use JavaScript though. Just link to #navigateHere.

Upvotes: 3

Christopher
Christopher

Reputation: 774

 window.location = "#navigateHere";

Upvotes: 7

Related Questions