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
:
http://i.imgur.com/GrjpL85.png
My question is, how to make the above selector return the list of the elements with xml:space
attribute?
Thanks!
The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-).
To select elements by an attribute name, pass a selector with the attribute's name to the querySelectorAll() method, e.g. document. querySelectorAll('[title]') . The querySelectorAll method will return a collection of the elements that have the provided attribute set.
The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.
You need to escape the colon
document.querySelectorAll('[xml\\3A space]')
I used https://mothereff.in/css-escapes to get the code above :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With