Let\'s have html code like this:
1.Hallo Kitty, How are you?<
this will work for you
$('#d1')
.contents()
.filter(function() {
return this.nodeType == Node.TEXT_NODE;
}).text()
or you can use this as suggested below for old browser support also
var text = $("#d1").contents().filter( function() {
return this.nodeType === 3;
}).text();
Demo
http://jsfiddle.net/SGZW4/
var text = $("#d1").contents().filter( function() {
return this.nodeType === 3;
}).text();
Improving on halex's trick, you can take advantage of the fact that .children()
finds only DOM nodes and ignores text nodes:
var text = $('#d1').clone().children().remove().end().text(); // string "Hallo "
...but I prefer the .nodeType
technique, because it's more clear what you're doing.