0

I have output from a CMS where I need to add a style to a certain character in the string. For instance, my output is:

 <div class="date">12 // 14 // 2013</div>

How can I add:

<span style="slashColor"> 

to the two double slashes so that my result would be:

<div class="date">12 <span class="slashColor">//</span> 14 <span class="slashColor">//</span> 2013</div>
3
  • 1
    And what have you tried? Why not do it with server side code? Commented Oct 15, 2013 at 21:11
  • Collect up the HTML inside the date div. Modify it to have the new span tags and put it back. Commented Oct 15, 2013 at 21:13
  • As long as you know that the double-slashes will occur in .date objects, you can use jQuery to iterate through them with $(".date") and replace each occurrence of // with <span class="slashColor">//</span> Commented Oct 15, 2013 at 21:16

2 Answers 2

3

Try this:

var original = $('.date').text();
var new_version = original.split('//').join('<span class="slashColor">//</span>');
$('.date').html(new_version);

Fiddle

If you have many div like the example you posted, you can use this:

$('.date').each(function () {
    var original = $(this).text();
    var new_version = original.split('//').join('<span class="slashColor">//</span>');
    $(this).html(new_version)
});

Fiddle

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

1 Comment

@Sergio hey Sergio , could you please let me know how do I split string by vertical pixel size from top for example each 200 px add a break span , thanks
1
var elements = document.getElementsByClassName('date');
for (var i = 0, e; e = elements[i++]; ) {
   e.innerHTML = e.innerHTML.replace(/\/\//g, '<span class="slashColor">//</span>');
}

or the jQuery way:

$('.date').each(function () {
  $(this).html($(this).html().replace(/\/\//g, '<span class="slashColor">//</span>'));
}

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.