Reputation:
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
Upvotes: 1
Views: 161
Reputation: 34107
Hiya simple demo (new updated for your specific case) http://jsfiddle.net/zrmGm/
http://jsfiddle.net/XswN4/ And demo when you can have 3 words and it will still pick up 2 http://jsfiddle.net/XswN4/1/
Updated demo based on what yo unmentioned this will help http://jsfiddle.net/zrmGm/ (this sample will get youi everything before <br>
tag!
using match
further read: http://www.jquery4u.com/syntax/jquery-basic-regex-selector-examples/
code
$.fn.ready(function() {
$('a').each(function() {
var str = $(this).html().match(/\w+\s\w+/);
alert(str);
});
});
Updated code to get everything before <br>
tag
$.fn.ready(function() {
$('a').each(function() {
var str = $(this).html().split('<br>')[0]
alert(str);
});
});
Upvotes: 0
Reputation: 42497
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: http://jsfiddle.net/cHhU5/
Upvotes: 3