10

I would like to create an HTML button that acts like a link to an item on the same page. So, when you click the button, it redirects to item on the same page.

How can I do this? (I would limit the solution to HTML, CSS, and JavaScript, because currently I am not using any other language)

Current Button (Bootstrap):

<a class="btn btn-large btn-primary" href="">Democracy</a>
itmilos
  • 489
  • 2
  • 8
  • 20

8 Answers8

9

Try:

<button onclick="window.location.href='location'">Button Name</button
Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
5

This is assuming that you are not talking about scrolling down to a regular anchor, and instead you want to scroll to actual HTML elements on the page.

I'm not sure if jQuery counts for you, but if you're using Bootstrap, I imagine it does. If so, you can bind to the "click" event for your button and put some javascript code there to handle the scrolling. Typically you might associate the link/button with the element you want to scroll to using a "data" attribute (e.g. data-scroll="my-element-id").

Without jQuery, you'll have to make a function that contains the code as described, and put in an onclick attribute that references your function, and passes "this" as a parameter to your function, so you can get the reference to the link/button element that called it.

For the code to use to actually scroll to the corresponding element, check out this article:

How to go to a specific element on page?

Quick example without jQuery:

<a class="scrollbutton" data-scroll="#somethingonpage" 
 onchange="scrollto(this);">something on page</a>

<div id="somethingonpage">scrolls to here when you click on the link above</a>

<script type="text/javascript">
    function scrollto(element) {
        // get the element on the page related to the button
        var scrollToId = element.getAttribute("data-scroll");
        var scrollToElement = document.getElementById(scrollToId);
        // make the page scroll down to where you want
        // ...
    }
</script>

With jQuery:

<a class="scrollbutton" data-scroll="#somethingonpage">something on page</a>

<div id="somethingonpage">scrolls to here when you click on the link above</a>

<script type="text/javascript">
    $(".scrollbutton").click(function () {
        // get the element on the page related to the button
        var scrollToId = $(this).data("scroll");
        var scrollToElement = document.getElementById(scrollToId);
        // make the page scroll down to where you want
        // ...
    });
</script>

Note: If you literally want a "button" rather than a "link", you can really use any element and make that clickable, e.g.:

<button class="scrollbutton" data-scroll="#somethingonpage">something on page</button>
Community
  • 1
  • 1
Andy Mudrak
  • 787
  • 3
  • 7
3

hey try this : -

  <button><a href="#A">Click Me</a></button>

then to which ever place you want to go in your site : -
u may just place the line below wherever you want,

  <a name="A"></a>

hope it works for you

Anurag-Sharma
  • 4,278
  • 5
  • 27
  • 42
0

Bookmark your item on the same page that you want to redirect to by assigning it an id. Assume id="itemId", then use<a class="btn btn-large btn-primary" href="#itemId">Democracy</a>. When you click the button, you will be redirected to the part of the page containing that item.

Janet
  • 1
  • 1
0
<a href="#sectionA">Read More</a>
<section id="sectionA">
    <p>You will be directed to this section. You can use id inside div/section/p tags etc</p>
</section>
Milos
  • 981
  • 3
  • 16
  • 41
0

which section or div using same id in <a href="?">

<a href="#democracy">Democracy</a>

div or section eg:

<section id="democracy">
your content
</section>

try this method abosolutly work

thwaha kp
  • 1
  • 2
-1

This is the easy way to do it

 <a href="link.html"> <button type="button""> Click </button>  </a>
Abdelhak
  • 176
  • 2
  • 12
  • wanted to get a functionality of using the button as a link with an ID tag and this worked out for me. – Ansub Mar 11 '22 at 05:44
-1

try this following code :

<button><a href="#Link">Click Over Here</a></button>

then to which ever place you want to go in your site u may just place the line below wherever you want :

<a name="Link"></a>
mohamed ibrahim
  • 567
  • 4
  • 12