Marie J Marie J - 30 days ago 6
jQuery Question

Extracting some part of text from a tag with jQuery

I have the following:

<a title="Home" href="#">Home<br>test</a>
<a title="Home" href="#">Home two<br>test</a>
<a title="Home" href="#">Home two three<br>test</a>

What I need is to get the text "Home" and "Home two" and "Home two three" from the address tags. I had forgotten that I can have a br tag in between.

I need something like this:

var abc = a.html()

but I know this only gives me everything

Answer Source

You can use .contents().filter(function() { return this.nodeType == 3; }).first().text() to get the first text node value. This will throw out any text after that (like your br and on). Here's an example: