views:

481

answers:

2

I am trying to get the text inside an element and I only want to get the text if it's inside the first-child of a placehoder div or if there are no childrent and it's only text inside.
So the two scenarios are:

<div id="wrap">text1</div>

and

<div id="wrap"><b>text1</b><b>text2</b></div>

So In both cases I want to get back "text1"
I know how to do it with 2 different queries but I am trying to unite them into one

$("#wrap :first-child").text()
$("#wrap").text()
+5  A: 
$("#wrap :first-child").text() || $("#wrap").text()
ohnoes
+1  A: 

As jquery selector expressions give results in document order, the expressions can be combined.

$('#wrap :first-child, #wrap').filter(':last').text()
Lachlan Roche