Run

Tutorial With Example

Run your live code

 
23
 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<title>Javascript Getting Element's Attribute Value</title>
6
​
7
</head>
8
<body>
9
    <a href="https://www.google.com/" target="_blank" id="myLink">Google</a>
10
​
11
<script>
12
    // Selecting the element by ID attribute
13
    var link = document.getElementById("myLink");
14
    
15
    // Getting the attributes values
16
    var href = link.getAttribute("href");
17
    alert(href); // Outputs: https://www.google.com/
18
    
19
    var target = link.getAttribute("target");
20
    alert(target); // Outputs: _blank
21
</script>
22
</body>