How to stop jQuery from returning tabs and spaces from formated code on .html() .val() .text() etc.

Posted by brandonjp on Stack Overflow See other posts from Stack Overflow or by brandonjp
Published on 2010-04-06T20:51:35Z Indexed on 2010/04/06 21:53 UTC
Read the original article Hit count: 181

Filed under:
|
|
|
|

I've got an html table:

<table><tr>
 <td>M1</td>
 <td>M2</td>
 <td>M3</td>
 <td>M4</td>
</tr></table>

and a simple jQ script:

$('td').click(function(){ alert( $(this).html() ); });

That works just fine.... but in the real world, I've got several hundred table cells and the code is formatted improperly in places because of several people editing the page.

So if the html is:

     <td>
                     M1         

             </td>

then the alert() is giving me all the tabs and returns and spaces:

Javascript Alert

What can I do to get ONLY the text without the tabs and spaces? I've tried .html(), .val(), .text() to no avail. Thanks!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about html