force existing link to open in a new window with javascript

here is some javascript that grabs an existing link and then forces it to open in a new tab when clicked.


<div class='my-test-link'><a href='//'></a></div>


// get the element
var my_test_link = document.getElementsByClassName("my-test-link");
// add an event listener to the click and then a function to run rather than the default action
my_test_link.addEventListener('click', function() { 
  var href = document.querySelector('a').href;
 }, false);


echo "moo";

Other Items in javascript
Page Views

This page has been viewed 52 times.

Search Code
Search Code by entering your search text above.

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.


Random Quote
Ever tried. Ever failed. No matter. Try again. Fail again. Fail better. You won't believe what you can accomplish by attempting the impossible with the courage to repeatedly fail better.