Skip to content

Multiple href in one link example | JS Code

  • by

You can’t Multiple href in one link HTML alone, because there’s only a single href attribute. Use the window.open() method in JavaScript:

Multiple href in one link In JavaScript

Complete example code:

In Anchor tag

<html>
<body>

	<a href="#" onclick="window.open('http://google.com'); window.open('http://yahoo.com');">

</body>
</html>

In Script

In function call window.open(‘url’). Example code creates a multiple links for a single <a href> tag.

<!DOCTYPE HTML>
<html>

<body>
	<a href="#" onclick="yourlink()">Click Here</a>

	<script>

		function yourlink() {

			var locs = ['site.com','site2.com','site3.com'] 

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

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

Output:

Multiple href in one link example

Do comment if you have any doubts or suggestions on this JS code.

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 *