Skip to content

How to add two links to one button | Example code

  • by

If you are want to open a 2 link with a single click, then use JavaScript. You can also use the anchor tag and onclick attribute to open multiple links.

Add two links to one button in HTML

<!DOCTYPE HTML>
<html>

<body>
	<button onclick="yourlink()">Click Here</button>

	<script>

		function yourlink() {

			var locs = ['http://google.com', 'http://yahoo.com'] 

			for (let i = 0; i < locs.length; i++) {
				window.open(locs[i])}
			};

		</script>
	</body>
</html>

Output:

add two links to one button

Without using script

HTML anchor tag onclick add URLs to open 2 it on single click.

<!DOCTYPE HTML>
<html>

<body>
	<a href="#" onclick="window.open('http://google.com');
    window.open('http://yahoo.com');">Click to open Google and Yahoo</a>
</body>
</html>		

Do comment if you have any doubts and suggestions on this HTML link topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *