How to get the value of the target attribute of a link in JavaScript?


In this tutorial, we will learn how to get the value of the target attribute of a link in JavaScript.

The target attribute specifies where to open the linked document or page.

By default, its value is set to ‘_self,’ which means the linked document should open in the same window or tab. It can also have values like ‘_blank,’ ‘_self,’ ‘_parent,’ ‘_top,’ and ‘frame_name’, where each value defines a different location to open the linked document.

Using the target property

To get the value of the target attribute of a link in JavaScript, use the target property. The target attribute is used to set where you want the linked document to open i.e. in the same window or new window or same frame, etc.

We can use the document.getElementById() method to get an HTML element. This method takes the id of an element as a parameter and returns an element object. From that object, we can get the target attribute value of that element by using the ‘target’ property.

Syntax

document.getElementById('mylink').target

In the above syntax, ‘mylink’ is the link's id (e.g. anchor tag) and by using document.getElementById() method and ‘target’ property, we get the target attribute value from that link.

Example 1

You can try to run the following code to get the value of the target attribute of a link −

<!DOCTYPE html> <html> <body> <p><a id="anchorid" rel="nofollow" target= "_blank" href="https://www.tutorialspoint.com/">tutorialspoint</a></p> <script> var myVal = document.getElementById("anchorid").target; document.write("Value of target attribute: "+myVal); </script> </body> </html>

Example 2

In the below example, we have used document.getElementById() method and target property to get the value of the target attribute of two different links.

<html> <body> <div> <p> Click on "Get target atribute" button to diisplay the target attribute of links </p> <a id="link1" target="_self" href="https://www.tutorialspoint.com/" >Link 1</a><br> <a id="link2" target="_blank" href="https://www.tutorix.com/" >Link 2</a> </div> <br /> <div id="root"> </div> <button onclick="getLink()"> Get target atrribute </button> <script> function getLink(){ // getting the target attribute value of anchor tags let target1 = document.getElementById('link1').target let target2 = document.getElementById('link2').target // outputting the target values let root = document.getElementById('root') root.innerHTML = 'Link 1 target attribute: ' + target1 + '<br>' root.innerHTML += 'Link 2 target attribute: ' + target2 + '<br>' } </script> </body> </html>

Using the getElementsByTagName() Method

In JavaScript, the document.getElementsByTagName() method can be used to get the value of the target attribute of a link or anchor tag. It takes a tag name in the parameter and returns an HTMLCollection, similar to a list or array. It contains all of the element objects of that tag name, and from each object, we can also get the value of the target attribute by using the property ‘target’.

Syntax

// getting all anchor tags
let links = document.getElementsByTagName('a')
// looping through all the HTMLCollection links
for (let index=0; index<links.length; index++){
   // accessing the target attribute from each element
   let target = links[index].target
   console.log(target)
}

In the above syntax, document.getElementByTagName() method takes ‘a’ as an argument, so it returns all of the elements which are anchor tags in an HTMLCollection, and looping through it, we get the target attributes values from all links and console logging it.

Example 3

In the below example, we have used a document.getElementByTagName() method to get the value of the target attribute from a link.

<html> <body> <p> Get the value of the target attribute of a link in JavaScript using <i> document.getElementsByTagName() </i> method </p> <div> <a target="_self" href="https://www.tutorialspoint.com/" >Link 1</a><br> <a target="_blank" href="https://www.tutorix.com/" >Link 2</a> </div> <br /> <div id="root"> </div> <button onclick="getLink()"> Get target attribute </button> <script> function getLink(){ let root=document.getElementById('root') let links=document.getElementsByTagName('a') for (let index=0; index<links.length; index++) { let target=links[index].target root.innerHTML+= 'Link '+(index+1)+' target: '+target+'<br>' } } </script> </body> </html>

Using the querySelectorAll() Method

In JavaScript, the document.querySelectorAll() method can be used to get the value of the target attribute of a link or anchor tag.

Syntax

Following is the syntax to get all anchor tags that have target attribute −

document.querySelectorAll('a[target]')

In the above syntax, document.querySelectorAll() method takes ‘a[target]’ as an argument. Hence, it returns all of the elements, which is an anchor tag containing the target attribute in a NodeList and looping through it, we can get all the target attribute values.

Example

In the below example, we have used the document.querySelectorAll() method to get the value of the target attribute of a link.

<html> <body> <p> Get the value of the target attribute of a link in JavaScript using <i> document.querySelectorAll() </i> method </p> <div> <a target="_self" href="https://www.tutorialspoint.com/" >Link 1</a><br> <a target="_blank" href="https://www.tutorix.com/" >Link 2</a><br> <a href="https://www.tutorialspoint.com/" >Link 3(no target)</a> </div> <br /> <div id="root"> </div> <button onclick="getLink()"> Get target Link </button> <script> function getLink(){ let root=document.getElementById('root') let links=document.querySelectorAll('a[target]') for (let index=0; index<links.length; index++) { let target=links[index].target root.innerHTML += 'Link '+(index+1)+' target: '+target+'<br>' } } </script> </body> </html>

Updated on: 15-Sep-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements