JavaScript htmlentities French

I have a .NET MVC page with a list of elements, each of which has <%: %> encoded descriptions in rel . I want to be able to search all elements with rel , which contains my search query.

One of the fields matters with htmlentities rel='D&eacute;coration'

I find "Décoration" in the search box, let jQuery look for all elements that have the rel attribute that contains the (indexOf! = -1) value:

No results!

Why? because Décoration != D&eacute;coration .

What would be the best solution to compare the two? (Should work for all special characters with an accent, not just &eacute; )

PS (I tried escape / unescape on both sides, also tried the trick to add it to the div and then read it as text, this replaces the dangerous stuff, but does not replace é (it isn’t because it is valid in utf -8 in anyway))

+6
javascript jquery model-view-controller unicode
source share
3 answers

Since &eacute; etc. are html objects, you can set the html content of a temporary div with a malformed string and extract the decoded string using the text content of the element. The browser will do the decoding work.

Using jQuery:

 function searchInRel(needle) { return $('[rel]').filter(function(i,e) { var decodedText = $('<div/>').html(e.attr('rel')).text(); return (decodedText.indexOf(needle) != -1); }; } 

Using only the DOM:

 function decodeEntities(text) { var tempDiv = document.getElementById('tempDiv'); tempDiv.innerHTML = text; return tempDiv.textContent; } 
+2
source share

If you serve your UTF-8 encoded pages, you do not need to use entities for all accented characters. The problem is resolved.

+1
source share

You can decode html objects. Just copy the two javascript methods from HERE

 var decoded = 'Décoration'; var entity = html_entity_decode('D&eacute;coration'); console.log(decoded == entity); 
0
source share

All Articles