jQuery selector where text = some value

You can create your own selector methods

For example, if you want to be able to do the following:

$('.js-rating-label:hasText(unrated)');

You can define the hasText method as follows

$.expr[':']['hasText'] = function(node, index, props){
  return node.innerText.contains(props[3]);
}

props[3] contains the text inside the brackets after ‘:hasText’.

Leave a Comment