Javascript – jQuery: Returning the text from first-child or self

javascriptjquery

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

Best Answer

$("#wrap :first-child").text() || $("#wrap").text()