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

+4
source share
6 answers

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); 

Fiddle: http://jsfiddle.net/maniator/Cba4m/3/

+3
source
 var text = []; $('.ms-vb').each(function() { text.push($(this).html().split('<br>')[1]); }); 
+6
source

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!

+2
source

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.

0
source

This makes a little more sense to me:

 var text = $('.ms-vb').clone(); text.children('span').remove(); text.children('br').remove(); name = text.html() alert(name); 

http://jsfiddle.net/vVHwX/1/

0
source

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"] 

jsFiddle here

Edit: I don't know why . map () does not receive love ... this is pretty much what it was created for.

0
source

Source: https://habr.com/ru/post/1382506/


All Articles