Array of elements in string

2

How to transform this object array from td's

localidades = "<td style='white-space: nowrap;'>Sto.amaro</td><td style='white-space: nowrap;'>Osasco</td><td style='white-space: nowrap;'>Lapa</td><td style='white-space: nowrap;'>Osasco Ii</td><td style='white-space: nowrap;'>Sao Miguel</td><td style='white-space: nowrap;'>Pirituba</td><td style='white-space: nowrap;'>Santana</td>";

then I use

console.log($(localidades).text(''));

and it returns me this array below

[<td style=​"white-space:​ nowrap;​">​​</td>​, <td style=​"white-space:​ nowrap;​">​​</td>​, <td style=​"white-space:​ nowrap;​">​​</td>​, <td style=​"white-space:​ nowrap;​">​​</td>​, <td style=​"white-space:​ nowrap;​">​​</td>​, <td style=​"white-space:​ nowrap;​">​​</td>​, <td style=​"white-space:​ nowrap;​">​​</td>​]

I would like to turn this array back into string only with the empty tds as in the string below

"<td style='white-space: nowrap;'></td><td style='white-space: nowrap;'></td><td style='white-space: nowrap;'></td><td style='white-space: nowrap;'></td><td style='white-space: nowrap;'></td><td style='white-space: nowrap;'></td><td style='white-space: nowrap;'></td>"
    
asked by anonymous 05.05.2015 / 16:47

2 answers

2

You can use the .each function by concatenating the element's HTML in a variable:

var $localidades = $("<td style='white-space: nowrap;'>Sto.amaro</td><td style='white-space: nowrap;'>Osasco</td><td style='white-space: nowrap;'>Lapa</td><td style='white-space: nowrap;'>Osasco Ii</td><td style='white-space: nowrap;'>Sao Miguel</td><td style='white-space: nowrap;'>Pirituba</td><td style='white-space: nowrap;'>Santana</td>");

$localidades.text('');

var html = "";
$localidades.each(function(i, elm) {
  html += elm.outerHTML;
});

$("#result").text(html);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="result">
</div>
    
05.05.2015 / 17:17
1

If you want to have a string with these empty TDs you can do so to empty them:

$(localidades).map(function(){
    this.innerHTML = '';
    return this;
});

But then you need a dummy element to have this HTML and you can extract it in a string. An example would be:

var limpo = $(localidades).map(function () {
    this.innerHTML = '';
    return this;
});
var tr = $('<tr />').html(limpo);
alert(tr[0].innerHTML);

jsFiddle: link

    
05.05.2015 / 22:18