4

How can I convert the characters of a div into spans?

For example, I'd like to convert this:

<div>
    Hello World
</div>

Into this:

<div>
    <span>H</span>
    <span>e</span>
    <span>l</span>
    <span>l</span>
    <span>o</span>
    <span>W</span>
    <span>o</span>
    <span>r</span>
    <span>l</span>
    <span>d</span>
</div>

I've tried this StackOverflow suggestion, but that converts spaces into spans. What I need is to convert only characters to spans:

$("div").each(function (index) {
    var characters = $(this).text().split("");

    $this = $(this);
    $this.empty();
    $.each(characters, function (i, el) {
        $this.append("<span>" + el + "</span");
    });
 });
jacefarm
  • 6,747
  • 6
  • 36
  • 46
MoHamed HaSsn
  • 555
  • 1
  • 6
  • 20
  • 2
    [`$('div').html(function(i, html) { return html.replace(/\S/g, '$&'); });`](https://jsfiddle.net/tusharj/tfb90wut/) – Tushar Dec 13 '16 at 07:11

7 Answers7

5

You can use String#replace method and html() method with a callback to reduce the code.

$("div").html(function(index, html) {
  return html.replace(/\S/g, '<span>$&</span>');
});

$("div").html(function(index, html) {
  return html.replace(/\S/g, '<span>$&</span>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  Hello World
</div>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
4

You can try with this simple JavaScript.

(function() {
  var div, i, span = "";
  div = document.querySelectorAll("div")[0];
  for (i = 0; i < div.innerText.length; i++) {
    if (div.innerText[i] !== " ") {
      span += "<span>";
      span += div.innerText[i];
      span += "</span>";
    }
  }
  div.innerHTML = span;
}());
<div>
  Hello World
</div>
1

I'd prefer to use regular expression:

var txt = $('#container').text();
var newTxt = txt.replace(/\w/g,function(c){
  return '<span>'+c+'</span>';
})
$('#container').html(newTxt);
span {
  display:inline-block;
  background-color:#dfdfdf;
  color:#aaa;
  padding:3px;
  margin:3px;
  border-radius:3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
    Hello World
</div>
Paweł
  • 4,238
  • 4
  • 21
  • 40
1

var textWrapper = document.querySelector('h1');
textWrapper.innerHTML = textWrapper.textContent.replace(/\S/g, "<span class='letter'>$&</span>");
<h1>Hello world</h1>
0
$("div").each(function (index) {
var characters = $(this).text().split("");

$this = $(this);
$this.empty();
$.each(characters, function (i, el) {
if(el != ' ')
$this.append("<span>" + el + "</span");
});

put a condition for space

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
0

try:

$("div").each(function (index) {
  var characters = $(this).text().split("");
  characters = characters.filter(v => v != '');
  $(this).empty();
  for(var i =0; i < characters.length; i++) {
      $(this).append("<span>" + characters[i] + "</span");
  }    
});
aks7
  • 431
  • 3
  • 12
0

tried to write as little as I can

html

<div>
    HelloWorld
</div>

js

var d=$("div");
var text=d.text();
text=$.trim(text);
d.empty();

for(i=0;i<text.length;i++){
    var span=$("<span></span>");
    span.text(text[i]);
    d.append(span)
}
Liang
  • 507
  • 2
  • 9