How to get the text after?
I have the following html format,
<tr> <td width="80%" style="padding-bottom: 3px" class="ms-vb"> <span class="ms-announcementtitle"> title is here 3 </span> <br> by Ahmed 1 </td> </tr> <tr> <td width="80%" style="padding-bottom: 3px" class="ms-vb"> <span class="ms-announcementtitle"> title is here 2 </span> <br> by Ahmed 2 </td> </tr> <tr> <td width="80%" style="padding-bottom: 3px" class="ms-vb"> <span class="ms-announcementtitle"> title is here 3 </span> <br> by Ahmed 3 </td> </tr>
how to use jQuery to get text after <br>
ie by Ahmed 1, by Ahmed 2, by Ahmed 3
This should work:
var td_clone = $('.ms-vb').clone(); $('span', td_clone).remove(); alert($.trim(td_clone.text()));
Fiddle: http://jsfiddle.net/maniator/Cba4m/
About your update:
var all_text = ''; $('.ms-vb').each(function(){ var td_clone = $(this).clone(); $('span', td_clone).remove(); all_text += '' + $.trim(td_clone.text()) + ' '; }); alert(all_text);
You can do the following:
$('.ms-vb').each(function(){ var str = $(this).html(); var output = str.substr(str.indexOf("<br>") + 4); console.log(output); });
Spell here: http://jsfiddle.net/FT3Fv/2/
Hope this helps!
This will do the trick:
jQuery('.ms-vb').html().match(/<br>([^<]*)/)[1]
I made jsbin for demo purposes:
http://jsbin.com/ifufuv/11/edit
It does not currently parse spaces, but it would be trivial to add this.
If I was going to make the clone method, I would do the following:
var result = $(".ms-vb").map(function() { return $.trim($(this).clone().find("span").remove().end().text()); }).get();
or if I'm going to make a regex version, I would do the following:
var result = $(".ms-vb").map(function() { return $(this).html().match(/<br\w*\/?>\s*(.*)\s*$/)[1]; }).get();
Both of them will return an array of strings:
["by Ahmed 1", "by Ahmed 2"]
Edit: I don't know why . map () does not receive love ... this is pretty much what it was created for.