0

anyone know how to write css selector to find a element that include exactly text. I have tried label:contains("^ab$") or label[innertext="ab"] or label[text="ab"] none of them works

<div>
<label>ab</label>
<label>ab cd</label>
<label>ab asd cd</label>
</div>

I only want to find the element <label>ab</label>

sangbe1192
  • 54
  • 8
  • If possible, changing the markup to be ` – Taplar Aug 12 '20 at 19:15

2 Answers2

0

It is not possible through CSS.

Read here for more info.

https://stackoverflow.com/a/1520469/7030091

Basically they thought about adding it to CSS3 but didn't as it would likely be hard to make it effient.

You can do this through Javascript however. The previous link also has an example of this.

hobomaan
  • 151
  • 6
0

If you can use jquery, you could just filter the elements that contain the specific string, using the filter function:

function getElementWithValue(elems, value){
    return elems.filter(function(index){
        return $(this).text() == value;
    });
}

$(document).ready(function(){
    var resultLabel = getElementWithValue($('label'), 'ab');
});
<div>
<label>ab</label>
<label>ab cd</label>
<label>ab asd cd</label>
</div>
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29