I want to remove the spaces on both sides of the matrix (split with tab), why only the last line is executed and meets the requirements

function takeout(th) {

var text2 = $("-sharpdata_matrix").val();//

var allcon2 = text2.split("\n");//

for (var i =0; i < allcon2.length; iPP) {//

a = allcon2[i].split("\t");  //tab

var ha=a.join("\t");//tab

// console.log(ha);

$(th).val($.trim(ha));
//
}
return true;//
}



textarea  
<textarea onblur = "takeout(this);" id="data_matrix" rows="20" cols="100" style=" height:218px;min-height:218px;width: 100%;margin-bottom: 20px;">ID Zygote 2_cell 4_cell 8_cell Morula ICM
Pou5f1 1 6 0.6 0.1 1 6
Sox2 2 5 0.5 0.2 2 6651
Gata2 3 4 0.4 0.3 3 46
cMyc 4 3 0.3 0.4 4 36
Tet1 5 2 0.2 0.5 5 25
Tet3 6 1 0.1 0.6 6 1</textarea>



$(th). Val ($.trim (ha)) here every time the contents of the textarea are emptied and replaced with the contents of the current loop, so the result is only the last record.

The

solution is to start by emptying the content, and then loop by adding to the original content each time, rather than directly setting

considering the performance problem, it is best to concatenate strings first in the loop, and then unify val ()

.
var text2 = $('-sharpdata_matrix').val();//

var allcon2 = text2.split("\n");//

$(th).val('');    // 

for (var i =0; i < allcon2.length; iPP) {//

a = allcon2[i].split('\t');  //tab

var ha=a.join("\t");//tab

// console.log(ha);

$(th).val($(th).val() + $.trim(ha) + '\n');
// 
//
}
return true;//
Menu