Search code examples
javascriptcssattributes

Select elements by attributes with ":" (colon)


In my project, there's a case where a library generates elements, and I need to select specific elements from there - which happen to contain an attribute with ":".
In other words, I ended up attempting to select using: document.querySelectorAll("[xml:space]").
But, when tested in Chrome, it didn't work, nor selecting using document.querySelectorAll("['xml:space']") - they both threw a DOMException:
https://i.sstatic.net/AHIeB.png

My question is, how to make the above selector return the list of the elements with xml:space attribute?
Thanks!


Solution

  • You need to escape the colon

    document.querySelectorAll('[xml\\3A space]')
    

    I used https://mothereff.in/css-escapes to get the code above :)