Let’s say that instead of looking at the ID, I want to select all a.href
elements that contain (a piece of) this example: if document.url
is "www.youtube.com"
.
var youtubeID = document.querySelectorAll('.youtubelink');
var youtubeDiv = document.querySelectorAll('a[href^="http://www.youtube.com"]');
New contributor
Mediamash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
You can use *
wildcard which matches any attribute value where https://www.youtube.com
appears anywhere in the string.
Use this selector
a[href*="https://www.youtube.com"]
var youtubeDiv = document.querySelectorAll('a[href*="https://www.youtube.com"]');
console.log(youtubeDiv.length);
<a href="https://www.youtube.com/sfdsfdsfsd">Link 1</a>
<a href="https://example.com?link=https://www.youtube.com/sfdsfdsfsd">Link 2</a>