1

I want to split text to control it with html <span>

This my html code

<div class="siteLabel">Gallery/Technology/Music/Video/Business/Nature/People/Sports/Car/Fashion/Learn/Typography</div>

the result should be like this

<div class="siteLabel">
  <span>Gallery</span>
  <span>Technology</span>
  <span>Music</span>
  <span>Video</span>
  <span>Business</span>
  <span>Nature</span>
  <span>People</span>
  <span>Sports</span>
  <span>Car</span>
  <span>Fashion</span>
  <span>Learn</span>
  <span>Typography</span>
</div>

The problem is that the labels number not static it may be more or less, so i can't use something like .split('/')

2
  • 1
    why can't you use .split('/')? Commented Feb 3, 2015 at 4:58
  • using .split('/') require select every label by it's number Gallery [0] , Technology [1] and so on. but this text of labels not static it may changed so i can't select it using numbers. Commented Feb 3, 2015 at 5:01

5 Answers 5

3
var arr = $(".siteLabel").text().split("/");
var html="";
for (i = 0; i < arr.length; ++i) {
    html+= "<span>"+arr[i]+"</span>";
}
$(".siteLabel").html(html);

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

My preference would be to still use .split('/'). Here is how you could do it using plain JavaScript:

var el = document.querySelector('.siteLabel');
var text = el.textContent ? el.textContent : el.innerText;
var items = text.split('/');
var html = '';

items.forEach(function(text){  
  html += '<span>'+text+'</span>';
});

el.innerHTML = html;

Demo: http://jsfiddle.net/e97e4Lsd/2/

Comments

1

You can use a simple regex like

$('.siteLabel').html(function(i, html) {
  return html.trim().replace(/(.+?)(\/|$)/g, '<span>$1</span>')
})
.siteLabel span {
  margin-right: 5px;
  border: 1px solid grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="siteLabel">Gallery/Technology/Music/Video/Business/Nature/People/Sports/Car/Fashion/Learn/Typography</div>

1 Comment

Thanks Arun it's helpful but a small thing it gives one empty <span></span> at the end of the code.
1

Try

$(".siteLabel").html(function(_, words) {
  return words.match(/[^\/]\w+/g).map(function(word) {
           return $("<span>", {"html": word}).add("<br />")
         });
})

$(".siteLabel").html(function(_, words) {
  return words.match(/[^\/]\w+/g)
            .map(function(word) {
              return $("<span>", {"html": word}).add("<br />")
            });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <div class="siteLabel">Gallery/Technology/Music/Video/Business/Nature/People/Sports/Car/Fashion/Learn/Typography</div>

Comments

0

var items = $('.siteLabel').text().split('/');
    debugger;
$('.siteLabel').empty();
$.each(items, function(index, item){
    debugger;
    $('.siteLabel').append("<span>" + item)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.