Sign in
Log inSign up

How To Add _blank Or Open New Tab For Link In Markdown

Full of Dev's photo
Full of Dev
·Jul 21, 2021·

1 min read

There are some limitations when we’re working with markdown, one of them is adding property to our HTML tags. In this case I want to add _blank property on my tag to open a link in a new tab

Hack it in javascript

We don’t have control (mostly) with how our markdown will be rendered, but we can hack around it in javascript.

Let’s say you have an article with id “article” and want to get all the links inside it, here is how

  const _article = document.getElementById('article') 
  const _links = _article.getElementsByTagName('a')
  for (var i = 0; i<_links.length; i++) {
    _links[i].setAttribute('target', '_blank')
  }