Click event open link in new tab
To open a link in a new tab when a user clicks on it, you can use the target
attribute in the anchor tag (<a>
) and set its value to _blank
. Here's an example:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Click me!</a>
This will open the link in a new tab when the user clicks on it.
Alternatively, you can use JavaScript to achieve this. You can add an event listener to the anchor tag and use the window.open()
method to open the link in a new tab. Here's an example:
<a rel="nofollow" target="_blank" href="https://www.example.com" onclick="window.open(this.href, '_blank'); return false;">Click me!</a>
In this example, the onclick
event listener is triggered when the user clicks on the anchor tag. The window.open()
method is used to open the link in a new tab, and the return false
statement prevents the default link behavior from occurring.
You can also use jQuery to achieve this. Here's an example:
<a rel="nofollow" target="_blank" href="https://www.example.com" class="open-in-new-tab">Click me!</a>
<script>
$(document).ready(function() {
$('.open-in-new-tab').on('click', function(event) {
event.preventDefault();
window.open($(this).attr('href'), '_blank');
});
});
</script>
In this example, the jQuery code selects all elements with the class open-in-new-tab
and adds an event listener to them. When the user clicks on one of these elements, the event listener is triggered, and the window.open()
method is used to open the link in a new tab. The event.preventDefault()
statement prevents the default link behavior from occurring.