Multiple href in one link example | JS Code

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 create a multiple link 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 and suggestion 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.