ZiSean
ZiSean

Reputation: 137

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);

Upvotes: 6

Views: 10074

Answers (2)

Vladu Ionut
Vladu Ionut

Reputation: 8193

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);

Upvotes: 1

Thomas Van der Veen
Thomas Van der Veen

Reputation: 3226

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.

Upvotes: 10

Related Questions