0

The following div contains some text and tags. I want to add <span> tag to every letter in the div.

<div id="text">Hello, <span class="name">John</span></div>

I need my output as below:

<div id="text">
  <span>H</span>
  <span>e</span>
  <span>l</span>
  <span>l</span>
  <span>o</span>
  <span>,</span>
  <span class="name">J</span>
  <span class="name">o</span>
  <span class="name">h</span>
  <span class="name">n</span>
</div>

or

<div id="text">
  <span>H</span>
  <span>e</span>
  <span>l</span>
  <span>l</span>
  <span>o</span>
  <span>,</span>
  <span class="name">
     <span>J</span>
     <span>o</span>
     <span>h</span>
     <span>n</span>
  </span>
</div>

Guys most of your reply says that I haven't tried anything. For all you I have solution if the dosn't exist in the code. I have done the below even before posting the question.

<div id="text">Hello, John</div>

Answer:

<div id="text">
  <span>H</span>
  <span>e</span>
  <span>l</span>
  <span>l</span>
  <span>o</span>
  <span>,</span>
  <span>J</span>
  <span>o</span>
  <span>h</span>
  <span>n</span>
</div>

Javascript used:

var obj=$('#text');
var text=obj.text();
text=text.split("");
var parsed="";
obj.empty();
for(var i=0; i<text.length; i++)
{
var tag=document.createElement('span');
tag.setAttribute('class','vtag');
tag.innerHTML=text[i];
obj.append(tag);
}
vinoth kumar
  • 35
  • 1
  • 9

3 Answers3

3

I've (very recently) been in your shoes, not even knowing where to start. The magic words are "JavaScript DOM manipulation":

var text = document.getElementById("text");
var string = "Hello, John";
string.split("");
var i = 0, length = string.length;
for (i; i < length; i++) {
    text.innerHTML += "<span>" + string[i] + "</span>";
}

That just puts them all in spans. You should try giving it a try to get the <span class="name"> bit working on your own from here.

Danubian Sailor
  • 22,047
  • 37
  • 140
  • 217
ndugger
  • 6,925
  • 5
  • 30
  • 41
2

You can try something like this: http://jqversion.com/#!/LKQqeGh (with jQuery)

var $div = $('#text').clone().html('');
$('#text').contents().each(function(){
  var spanClass = '';

  if ($(this).is('span')) {
    spanClass = $(this).attr('class');
  }

  $textArray = $(this).text().split('');

  for (var i = 0; i < $textArray.length; i++) {
    $('<span>').addClass(spanClass).text($textArray[i]).appendTo($div);
  }
});

$('#text').replaceWith($div);
Mihai Matei
  • 23,635
  • 4
  • 32
  • 50
2

HTML :

<div id="text">Hello, <span class="name">John</span></div>

Pure Javascript :

var text = document.getElementById("text");
var msg = text.textContent;
var name = document.getElementsByClassName("name")[0].textContent;

// remove name from msg
msg = msg.substring(0,msg.length - name.length);
// to char array
msg = msg.split('');
name = name.split('');

text.innerHTML = "<span>" + msg.join("</span><span>") + "</span>";
text.innerHTML += "<span class=\"name\">" + name.join("</span><span class=\"name\">") + "</span>";

Demo JSFiddle

Apolo
  • 3,656
  • 1
  • 18
  • 48
  • your solution is good only if the last has tag. Also if there is only one in the string. What if there is more than one and if it is in different position. – vinoth kumar May 10 '14 at 05:07