1

So I have this:

<span class="a"> String <span class="b">Herp</span> </span>

and I need to define span.a's string as a variable without getting span.b's string like:

var a = $(".a").text();

How should I do it?

Jawn
  • 41
  • 5
  • Check this answer. This is what you want: http://stackoverflow.com/questions/298750/how-do-i-select-text-nodes-with-jquery#answer-298758 – Chandu Jun 18 '12 at 22:21

2 Answers2

4
var a = $(".a")[0].firstChild.nodeValue;
yankee
  • 38,872
  • 15
  • 103
  • 162
0

Demo

var txt1 = $(".a").contents().filter(function() {
  return this.nodeType == 3;
}).text();

or like:

var txt1 = $('.a')[0].childNodes[0].nodeValue;

Demo 2

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313