Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create "target","_blank" using JS HTML DOM

I would like to create _blank with js HTML DOM, is it correct?

var a = document.createElement('a');

a.appendChild(document.createTextNode(contacts[i][j]));

a.setAttribute("href","http://www.facebook.com/", contacts[i][j],"target","_blank" );

td.appendChild(a);
like image 728
ZiSean Avatar asked Jan 04 '23 10:01

ZiSean


2 Answers

I think you should spilt the setAttribute like:

a.setAttribute('href', 'http://www.facebook.com/');

a.setAttribute('target', '_blank');

According to the docs it takes only two parameters.

like image 196
Thomas Van der Veen Avatar answered Jan 07 '23 11:01

Thomas Van der Veen


The setAttribute method accept only 2 parameters

  1. name is the name of the attribute as a string.
  2. value is the desired new value of the attribute.

   var a = document.createElement('a');
   a.appendChild(document.createTextNode(contacts[i][j]));
   a.setAttribute('href', 'http://www.facebook.com/');
   a.setAttribute('target', '_blank');
   td.appendChild(a);
like image 32
Vladu Ionut Avatar answered Jan 07 '23 11:01

Vladu Ionut