Javascript DOM HTML Document querySelectorAll() Method by tag and attribute name

Introduction

Set the border of all <a> elements in the document that have a "target" attribute:

Click the button to add a red border to all links in the document with a target attribute.</p>

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
a[target] {//from  w w  w. jav a2 s .c o m
  background-color: yellow;
}
</style>
</head>
<body>

<p>The CSS selector a[target] makes sure that all links with a target attribute gets a yellow background:</p>

<a href="https://www.java2s.com">java2s.com</a>
<a href="http://www.disney.com" target="_blank">disney.com</a>
<a href="http://www.wikipedia.org" target="_top">wikipedia.org</a>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {
  var x = document.querySelectorAll("a[target]");
  var i;
  for (i = 0; i < x.length; i++) {
    x[i].style.border = "10px solid red";
  }
}
</script>

</body>
</html>



PreviousNext

Related