Problem
Using html() returns:
This is a cool test & <strike>stuff like</strike> that
Using text() , however, will return:
This is a cool test & stuff like that
So, html() needed in order to see the string, <strike> , but then, of course, all special entities are escaped as they should be. There are ways to crack this problem, but imagine what happens if, say, HTML itself is described in the text:
Use the <strike></strike> tags to strike out text.
In this case, you need an interpretation,
Use the <strike></strike> tag to strike out text.
That is why the only right way to approach this would be to iterate through the DOM nodes.
JQuery / DOM solution
Here's the jsFiddle of my solution, and here is the code:
jQuery.fn.indexOfTag = function (tag) { var nodes = this[0].childNodes; var chars = 0; for (var i = 0; nodes && i < nodes.length; i++) { var node = nodes[i]; var type = node.nodeType; if (type == 3 || type == 4 || type == 5) { // alert('advancing ' + node.nodeValue.length + ' chars'); chars += node.nodeValue.length; } else if (type == 1) { if (node.tagName == tag.toUpperCase()) { // alert('found <' + node.tagName + '> at ' + chars + ', returning'); return chars; } else { // alert('found <' + node.tagName + '>, recursing'); var subIndexOfTag = $(node).indexOfTag(tag); if (subIndexOfTag == -1) { // alert('did not find <' + tag.toUpperCase() + '> in <' + node.tagName + '>'); chars += $(node).text().length; } else { // alert('found <' + tag.toUpperCase() + '> in <' + node.tagName + '>'); chars += subIndexOfTag; return chars; } } } } return -1; }
Uncomment alert() to get an idea of ββwhat's going on. Here is a link to nodeType s.
Counting jQuery / DOM outerHTML parameters
Based on your comments, I think you say you want to count HTML tags (by character), but not just for HTML objects. Here is the new jsFiddle of the function itself, and here is the new jsFiddle of it in relation to your problem.