Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:
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!

like image 868
avi12 Avatar asked Jul 14 '17 20:07

avi12


People also ask

How do you select an element based on attributes and values?

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 (-).

How do I select an element by attribute name?

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.

How do you select a specific element?

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.


1 Answers

You need to escape the colon

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

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

like image 70
Ted Whitehead Avatar answered Oct 25 '22 20:10

Ted Whitehead