<!DOCTYPE HTML>
<
html
>
<
head
>
<
title
>
How to add target="_blank"
to a link using jQuery ?
</
title
>
<
script
src
=
</
script
>
</
head
>
<
body
style
=
"text-align:center;"
>
<
h1
style
=
"color: green;"
>
GeeksforGeeks
</
h1
>
<
h2
>
Click on the button to add
target="_blank" to the link
</
h2
>
<
div
id
=
"outer"
>
THIS IS LINK
</
a
>
</
div
>
<
br
>
<
button
onclick
=
"gfg_Run()"
>
Click Here
</
button
>
<
h2
id
=
"Result"
style
=
"color:green;"
></
h2
>
<
script
>
let res = document.getElementById("Result");
// This function returns all attribute properties
// of DOM element as a string and has nothing
// to do with the target property
function getAttr() {
let elmt = document.getElementById("a");
let attr = elmt.attributes, n = attr.length, str = "";
for (let i = 0; i <
n
; i++) {
str
= str + attr[i].nodeName + "='"
+ attr[i].nodeValue + "'<br>";
}
return str;
}
res.innerHTML = getAttr();
function gfg_Run() {
// Getting the anchor element inside the outer DIV.
let el = document.getElementById('outer')
.getElementsByTagName('a');
for (let i = 0; i <
el.length
; i++) {
// Set the target property of every anchor
// element inside the outer DIV
el[i].setAttribute('target', '_blank');
}
res.innerHTML
=
getAttr
();
}
</script>
</
body
>
</
html
>