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.

HTML

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

Javascript

// 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;
  alert(href);
 }, false);

PHP

echo "moo";

Other Items in javascript
Page Views

This page has been viewed 110 times.

Search Code
Search Code by entering your search text above.
Welcome

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
Don't look to be the next Facebook, try to solve a real problem instead.
Unknown