This should be real easy. Given below is the HTML.
<div id='attachmentContainer'>
#Attachment#
<span id='spnAttachmentName' class='hidden'>#AttachmentName#</span>
<span id='spnAttachmentPath' class='hidden'>#AttachmentPath#</span>
</div>
I want to get just the #Attachment# and not the other text. When I tried
$("#attachmentContainer").text()
it gives out all #Attachment#, #AttachmentName# as well as #AttachmentPath#. I know I could just put #Attachment# into another span and access it directly but I was just intrigued on how to do this. Any help is much appreciated.
Since your text happens to be the first child node of the
<div>:The
nodeTypecheck is meant to be a safeguard – it makes sure you are actually handling a text node – thefirstChildmight be something different after all. React accordingly, this is just an example.To retrieve the value of all text children (or a specific one), just loop over the
childNodescollection of your element, concatenating all bits you find into a string: