views:

88

answers:

2

given the html:

<div>
    <span class="a">a
      <span class="b">b</span>
    </span>
<div>

Is it possible to just select the first span and not it's children?

Calling $('div span.a').text() prints the 'b' in the child node as well as the 'a'

+2  A: 
var span1 = $("span.a").clone();
$(span1).children().remove();
var text = $(span1).text();
Bradley Mountford
+1  A: 

You probably would be better off writing better structured html like:

<div>
    <span class="c">
      <span class="a">a</span>
      <span class="b">b</span>
    </span>
</div>

But you could do this:

var text = $('div span.a').clone().find('*').remove().end().text();
PetersenDidIt