I thought that this would be rather straightforward, but I think the keywords are just too general so I keep getting query results for things like this and this.
Basical
As the title request it, here's a possible way to get text nodes between spans:
var textNodes=$('#test').contents().filter(function(){
return this.nodeType == 3; // text node
});
It is also possible to manually check for consecutive spans that have no empty text node between them by comparing each node with the precedent one. Something like this will do the trick:
function combineSpansIn(selector, spanClass) {
// initialize precedent values
var prec=null;
var precNodeType;
$(selector).contents().each(function(){
if ($.trim( $(this).text() ) !== "") { // empty nodes will be useless here
var nodeType = this.nodeType;
// check if still a combinable span
if (nodeType == 1 && this.className==spanClass && nodeType == precNodeType) {
// append current node to precedent one
$(prec).append(" "+ $(this).text() );
// remove current node
$(this).remove();
} else {
// update precedent values
prec=this;
precNodeType = nodeType;
}
}
});
}
combineSpansIn('#test', 'highlighted');
Please take a look at this FIDDLE.