Trying to get the very first text node.
var s = $.trim('login<tbody> <tr> <td class="translated">Lorem ipsum dummy text</td></tr><tr><td class="translated">This is a new paragraph</td></tr><tr><td class="translated"><b>Email</b></td></tr><tr><td><i>This is yet another text</i></td> </tr></tbody>');
if($(s).find('*').andSelf().not('b,i').length > 1){
alert($(s).find('*:not(:empty)').first().text());
}
Here is a link to what i am doing on jsfiddle
It is not giving me the very first text node which is “login” rather it is givig me
“lorem ipsum…” which is 2nd text node.
NOTE:
The HTML string is dynamic so do not think of assigning class or id to it or enclosing it in some HTML element or substring etc.
If your HTML is already a string, I would just use
substring()to get all characters leading up to the first angled bracket:Update
This will find the first text node if it’s not at the beginning or
undefinedif there are no text nodes:Update 2
This could also work, though I haven’t looked closely at the repercussions of doing it: