What is a good way to get text from a jQuery element when the text itself is adjacent to another element containing text?
In this example, I want to get the text: βText I want,β ignoring the text in the adjacent child:
<span> <a>Text I want to ignore</a> Text I want </span>
My solution was to get all the text in the <span> tag and then remove all the text in the <a> tag. This is a bit inconvenient, so I'm wondering if there is a better way:
var all_the_text = $('span').text(); var the_text_i_dont_want = $('span').find('a').text(); var text_i_want = all_the_text.replace(the_text_i_dont_want, '');
javascript jquery
user1473339
source share