maxp
maxp

Reputation: 25141

jquery - Select no children

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'

Upvotes: 1

Views: 2212

Answers (2)

PetersenDidIt
PetersenDidIt

Reputation: 25620

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();

Upvotes: 4

Bradley Mountford
Bradley Mountford

Reputation: 8273

var span1 = $("span.a").clone();
$(span1).children().remove();
var text = $(span1).text();

Upvotes: 2

Related Questions