0

I've created the following code:

const circle = document.querySelectorAll('[src = "https://cdn-icons-png.flaticon.com/512/734/734544.png"]')
const classTwo = document.querySelectorAll('.one')

const circleANDClassTwo = document.querySelectorAll('.one [src = "https://cdn-icons-png.flaticon.com/512/734/734544.png"]')

//console.log(circle)
//console.log(classTwo)
console.log(circleANDClassTwo)
<img id="1" src="https://i.imgur.com/ZcGeIVz.png" class="one" style="width:50px;height:50px">
<img id="2" src="https://i.imgur.com/ZcGeIVz.png" class="one" style="width:50px;height:50px">
<img id="3" src="https://i.imgur.com/ZcGeIVz.png" class="one" style="width:50px;height:50px">
<img id="4" src="https://i.imgur.com/ZcGeIVz.png" class="one" style="width:50px;height:50px">
<img id="5" src="https://cdn-icons-png.flaticon.com/512/734/734544.png" class="one" style="width:50px;height:50px">
<img id="6" src="https://cdn-icons-png.flaticon.com/512/734/734544.png" class="one" style="width:50px;height:50px">
<img id="7" src="https://cdn-icons-png.flaticon.com/512/734/734544.png" class="two" style="width:50px;height:50px">
<img id="8" src="https://cdn-icons-png.flaticon.com/512/734/734544.png" class="two" style="width:50px;height:50px">
<img id="9" src="https://cdn-icons-png.flaticon.com/512/734/734544.png" class="two" style="width:50px;height:50px">
<img id="10" src="https://i.imgur.com/ZcGeIVz.png" class="two" style="width:50px;height:50px">
<img id="11" src="https://i.imgur.com/ZcGeIVz.png" class="two" style="width:50px;height:50px">
<img id="12" src="https://i.imgur.com/ZcGeIVz.png" class="two" style="width:50px;height:50px">

I know how to use the AND operator with the function querySelectorAll when it's selecting only specific tags and classes like described in this answer. But I didn't manage to make it filter a class AND a specific attribute at the same time. On my sample, I'd like to filter all elements that belong to the class one and that have a src https://stackoverflow.com/a/36545002/14739472 (that would be the id 5 and the id 6)... I've tried using ., space, , but none of them work... I didn't find any example on how to do it when I'm specifying an attribute AND a class with querySelectorAll. How can I do it?

raylight
  • 447
  • 4
  • 17
  • 1
    Just don't use any separator: `.one[src="https://cdn-icons-png.flaticon.com/512/734/734544.png"]` – CherryDT Sep 29 '21 at 07:30
  • Gosh, I really spent some hours here because of that... Thanks, that works perfectly for me :) – raylight Sep 29 '21 at 07:31
  • 1
    These are the same selectors that you can use in CSS, maybe this helps in finding documentation and examples next time! (eg. search for `css select both class and attribute`) – CherryDT Sep 29 '21 at 07:33
  • 1
    @raylight Don’t guess. The selectors are [specified](//drafts.csswg.org/selectors/#overview) and [documented](//developer.mozilla.org/docs/Web/CSS/CSS_Selectors). – Sebastian Simon Sep 29 '21 at 07:34

0 Answers0