Reputation: 31
I'm looking for a jQuery solution
<pre><marker id="markerStart"></marker>
aaaaa
<span style='font-family:monospace;background-color:#a0a0a0;'>bbb</span>bb
cc<marker id="markerEnd"></marker>ccc
</pre>
How get text between <marker>
tags? Should be the following result:
aaaaabbbbbcc
Example 2 (markerEnd inside SPAN tag):
<pre><marker id="markerStart"></marker>
aaaaa
<span style='font-family:monospace;background-color:#a0a0a0;'>b<marker id="markerEnd"></marker>bb</span>bb
ccccc
</pre>
expected result: aaaaab
Thanks.
Upvotes: 3
Views: 2559
Reputation: 185913
Here you go:
function textBetween(id1, id2) {
var node = document.getElementById(id1).nextSibling,
text = '';
while ( node && node.id !== id2 ) {
text += node.textContent.trim();
node = node.nextSibling;
}
return text;
}
Call this to get your text:
textBetween('markerStart', 'markerEnd')
Live demo: http://jsfiddle.net/simevidas/yxVxy/4/
Upvotes: 2
Reputation: 887415
You can get all elements between two nodes like this:
$('marker:first').nextUntil('marker').text()
However, since you need to include text nodes, you need to write
var contents = $('pre').contents(),
start = contents.filter('marker:first'),
end = start.nextAll('marker:first'),
startIndex = contents.index(start),
endIndex = contents.index(end);
alert(contents.filter(function(i) {
return i > startIndex && i < endIndex;
}).text());
http://jsfiddle.net/SLaks/2jEps/
Upvotes: 3