Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery/JavaScript - Trim white space/tab between two element? [duplicate]

Possible Duplicate:
Remove non breaking space ( ) from between elements using jquery

How to write a script to trim white space/tab between two element?
For example,

<tr>       <td>A     </td>                <td>B   </td>      <td>C    </td>       </tr>

Convert to,

<tr><td>A     </td><td>B   </td><td>C    </td></tr>

For the example, the script should remove the white space/tab between first <td>xxx</td> element and second <td>xxx</td> element and so on.

Thanks

like image 291
Charles Yeung Avatar asked Mar 14 '26 23:03

Charles Yeung


2 Answers

Use:

function specialTrim(str){
    return str.replace(/>\s+</g,'><');
}
like image 179
Darm Avatar answered Mar 17 '26 12:03

Darm


You can use contents() with filter() to match the text nodes inside your <tr> element:

$("tr").contents().filter(function() {
    return this.nodeType == 3;
}).remove();
like image 38
Frédéric Hamidi Avatar answered Mar 17 '26 13:03

Frédéric Hamidi