Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ...

like image 955
Rajesh Rs Avatar asked Dec 07 '22 18:12

Rajesh Rs


2 Answers

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";
};
like image 155
kevinji Avatar answered May 24 '23 22:05

kevinji


 window.location = "#navigateHere";
like image 20
Christopher Avatar answered May 24 '23 21:05

Christopher