LimaNightHawk
LimaNightHawk

Reputation: 7083

Need Css Selector with attribute containing special escape (apostrophe) character

I'm trying use a jQuery css selector to find an element using an attribute specification. The problem is that sometimes the attribute's value contains an apostrophe, like this:

<ul id="mylist">
    <li data-abrv="N">None</li>
    <li data-abrv="W'A">With'Apos</li>
</ul>

I've tried

$('#mylist li[data-abrv="W'A"]')

and

$('#mylist li[data-abrv="W\'A"]')

...but neither seems to work. Is this possible? If so, how?

Note: I cannot control the HTML content.

Upvotes: 0

Views: 1416

Answers (2)

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

$('#mylist li[data-abrv*="\'"]')  

see demo by this selector you can select all li which data-abrv contains ' single cot

Upvotes: 0

Joseph Marikle
Joseph Marikle

Reputation: 78520

It seems to work.

$('#mylist li[data-abrv="W\'A"]').css({color:'red'});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<ul id="mylist">
    <li data-abrv="N">None</li>
    <li data-abrv="W'A">With'Apos</li>
</ul>

Upvotes: 1

Related Questions