cheerio


Accessing the text of a class that contains other elements using Cheerio


I only want to access h1's text (H1 title is here in this case), but it prints everything. I tried adding .remove('.small-title') before text(), but it didn't work.
<div class="modal-know>
<h1>
H1 title is here
<div class="small-title">
Click
Click 2
</div>
</h1>
</div>
Node.js code
var newsTitle = $2('.modal-know h1').text(); // prints out everything
console.log(newsTitle);
have a look at cheerio docs: text()
it says
including their descendants
That is the same behaviour that jQuery .text()
So maybe this answer could help you :jQuery: using .text() to retrieve only text not nested in child tags
Here you have the code I tested:
let newsTitle = $('.modal-know h1').contents()[0].nodeValue;
// solution 2:
// .clone() //clone the element
// .children() //select all the children
// .remove() //remove all the children
// .end() //again go back to selected element
// .text(); // prints out everything
//solution 3:
// .contents().filter(function(){
// return this.nodeType == 3;
// })[0].nodeValue;
console.log(newsTitle);
*in your code sample ther is a missing " in the div modal-know class
<div class="modal-know> -> <div class="modal-know">

Related Links

confused about cheerio nextUntil
Get the text of the current node only
Accessing the text of a class that contains other elements using Cheerio
cheerio reading children of script tag
how to extract info in this kind of html using cheerio?
Cheerio not finding table content

Categories

HOME
sonarqube
google-app-engine
assembly
prism
developer-tools
wakanda
jax-rs
flex
swt
components
bitbucket-api
google-cloud-functions
alsa
square
trac
google-chrome-app
ycsb
google-form
mongodb-csharp
do-while
parameter-passing
http-post
azure-iot-hub
dynamic-sql
google-data-studio
jersey-1.0
git-extensions
pyramid
solrcloud
selinux
visual-glitch
guzzle
epsilon
android-sharedpreferences
inversion-of-control
angular-translate
cppreference
bioinformatics
boost-asio
powershell-ise
android-gridview
pdw-file-browser
fiware-cygnus
plaintext
watchface
sharp-snmp
print-css
plr
logback-groovy
chronicle-map
oracle-service-bus
file-sharing
pom.xml
user-defined-fields
dtsearch
vugen
corruption
edit
flac
universal-analytics
mathcad
rxtx
visual-studio-2008-sp1
xcode-server
segment-io
async.js
spring-cloud-feign
refinerycms
tracker-enabled-dbcontext
sklearn-pandas
vhosts
vigenere
eoferror
rapidsvn
uncaught-typeerror
paw
malformedurlexception
draw2d-js
vbe
multitasking
accesscontrolexception
gtm-oauth2
ui-select2
route-provider
server-administration
fragmenttransaction
web-farm
objectquery
database-create
agility.js
gobject-introspection
workflow-services
update-statement
webdev.webserver
luajava
aspmenu
eclipse-marketplace
visualj#
cldc
firefox-3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App