Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery firstChild.data equivalent

Is there an equivalient of the firstChild.data in JQuery?

Given the following HTML :

<p id='test'>Hello<span>world</span>/<p>

the below javascipt will return : "Hello"

document.getElementById('test').firstChild.data

the given JQuery will return : Hello< span>world</span>

$('#test').text()

how can I achieve this?

like image 856
Peter Szanto Avatar asked Mar 11 '26 01:03

Peter Szanto


1 Answers

You want a text node, so you can make use of .contents:

$("#test").contents().first().text();
like image 137
pimvdb Avatar answered Mar 12 '26 13:03

pimvdb