3
var foundin = $('*:contains("the")').last();

works on a page, but

var foundin = $('*:contains("&copy")').last();

returns nothing, even though '&copy' does appear in the page source. I tried escaping the '&', but that doesn't work either. Is there a way to use contains to find an html encoded character in a page?

Basically, what I want to do is find the element contains © and parse it to get the copyright holder. I asked a similar question aiming at this with regex here: select HTML text element with regex?

Community
  • 1
  • 1
tarayani
  • 193
  • 1
  • 9

1 Answers1

1

The quick and dirty:

var $div = $("<div>");

$.expr[":"].containsEnc = function(a, b, c, d) {
    var decoded = $div.html(c[3]).text(); // decode &copy to ©

    return ~a.textContent.indexOf(decoded); // does the element contain the character
};

Then you can use containsEnc as a selector:

$('body:containsEnc("&copy")').html('ok');

It may not be the safest and most performant, though.

http://jsfiddle.net/wfLur/1/

pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • Don't you think `... > -1` is clearer than `~...`? Just a thought. – Ry- Nov 26 '11 at 23:05
  • 1
    @minitech: Probably, but since `~` is basically never used it has gotten the meaning "contains" for me... – pimvdb Nov 26 '11 at 23:05
  • well, that does work, but it doesn't really help me with what I'm trying to do, since it returns the longest div element containing &copy, I think? I edited the original question, I'm basically trying to parse the copyright holder. – tarayani Nov 26 '11 at 23:22
  • @tarayani: No, not at all in fact. The div is just a temporary tool to make the browser decode `&copy`. You can use the selector to select all elements containing `&copy` (or some other character(s)). – pimvdb Nov 26 '11 at 23:24
  • @tarayani: This is a fiddle which is more related to your scenario I guess: http://jsfiddle.net/wfLur/6/. You can use your original selector, but with `containsEnc` instead of `contains`. – pimvdb Nov 27 '11 at 00:05