Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

How to use querySelector() with multiple attributes ?

Hello,

I'm trying to use querySelector() with multiple attributes and I don't understand why it doesn't work.

alert(document.querySelectorAll('article[data-id='+id+'][data-color='+color+']'));

It seems to be the data-color which generates the problem : it returns null whereas I the element exists in the DOM. But I really don't understand why.

Do you have any idea ?

Thanks ;).

V

0 likes
4 replies
Sergiu17's avatar
document.querySelector(`article[data-id='${id}'][data-color='${color}']`)
vincent15000's avatar

@Sergiu17 It's ok ... I search for the problem for 2 hours and it was only a little variable that wasn't initialized correctly. Sure, I'm tired ;).

It's not really the good answer, but it helped me to find the solution. ;)

Please or to participate in this conversation.