How to Link a Button to Another Page in HTML?
Last Updated :
17 Nov, 2024
Linking a button to another page is a common requirement in web development. It can allow the users to navigate between a website's different sections or pages. This can be done using the various HTML elements.
Prerequisites
1. Using <a> Tag Styled as Button
The <a> tag is traditionally used to create hyperlinks to navigate from one web page to another. By default, <a> tags can be displayed as clickable text links. However, by applying the CSS styles, we can make the <a> tag look and behave like a button.
Syntax
<a href="destination.html" class="button-class">Click Me</a>
HTML
<!--index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Styled Link as Button</title>
<style>
.button-class {
background-color: #4CAF50;
/* Green */
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
}
</style>
</head>
<body>
<a href="another-page.html" class="button-class">
Go to Another Page</a>
</body>
</html>
HTML
<!-- another-page-->
<!DOCTYPE html>
<html>
<head>
<title>Basic Another Example</title>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<h2>Basic Another Page Example</h2>
</body>
</html>
Output
2. Using <button> Tag with an onClick Event
The button element can be used to define the clickable button. By using the onClick attribute, we can specify the JavaScript code that will execute when the button is clicked. This method can directly change the windoe.location.href property to navigate to the another page.
Syntax
<button onclick="location.href='destination.html'">Click Me</button>
HTML
<!--index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Button with OnClick Event</title>
</head>
<body>
<button onclick="location.href='another-page.html'">
Go to Another Page
</button>
</body>
</html>
Output
3. Using JavaScript to Handle Button Click Events
Using the JavaScript to handle the click events can provides the greater control and flexibility. We can perform additional action before navigating to the another page, such as the validating data, displaying confirmation dialogs, or performing the asynchronous operations. This method can involves the assigning the onClick event handler directly via JavaScript.
Syntax
<button id="myButton" > Click Me </button>
<script>
document.getElementById("myButton").onclick = function () {
window.location.href = "destination.html";
};
</script>
HTML
<!--index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Event Listener Example</title>
</head>
<body>
<button id="navigateButton">Go to Another Page</button>
<script>
document.getElementById("navigateButton").onclick = function () {
window.location.href = "another-page.html";
};
</script>
</body>
<html>
Output
4. Using Form with a Submit Button
Form can be used to collect the user input and submit it to the server. We can use the form with a submit button to navigate to the another page. This approach can be particularly useful when you need to pass the form data to the another page or backend service for the processing.
Syntax
<form action="destination.html">
<input type="submit" value="Click Me">
</form>
HTML
<!---index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form Submit Example</title>
</head>
<body>
<form action="another-page.html">
<input type="submit" value="Go to Another Page">
</form>
</body>
</html>
Output
Similar Reads
How to Add Link to HTML Button? In HTML, links connect webpages using the <a> tag, with the href attribute specifying the destination URL. The simplest way to do this is to add a link to a button by wrapping it inside <a> tag.HTML<a href="https://www.geeksforgeeks.org/html-tutorial/"> <button>Click me</b
3 min read
How To Link Two Pages In HTML ? The linking of two pages in HTML is a fundamental aspect of web development, used in over 95% of websites for navigation. The most common method is through the <a> (anchor) tag, which creates hyperlinks to other pages or resources. In this article, we will explore various ways to link pages in
3 min read
How to redirect a page to another page in HTML ? Redirecting a page in HTML involves sending users to a different web page automatically or upon interaction. This can be achieved using the anchor tag's href attribute or the meta tag with the http-equiv attribute set to refresh and the content attribute specifying the time delay and destination URL
3 min read
How to Create Links to Other Pages in HTML ? HTML links are like pathways on the internet. They connect one webpage to another. Imagine a link as having two parts: a starting point (where you click) and an endpoint (where you end up). When you click on a link, you're basically starting at one point (the anchor) and moving to another (the desti
2 min read
How to use anchor tag to open links in HTML ? In this article, we will learn how to use anchor tags to open links in HTML and we will implement different operations using anchor tags in HTML. We will see in this article, how to redirect another website through an anchor tag and how to open a blank new tag for any new link to open an image in a
3 min read
How To Link Pages In HTML? In HTML, linking pages is the fundamental practice that allows users to navigate from one web page to the another within same website or to an entirely different website. These links, also known as hyperlinks, are created using the <a> tag, which can direct the users to another HTML document,
5 min read