DEV Community

Full of Dev
Full of Dev

Posted on • Originally published at onebite.dev

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

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')
  }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)