Tuesday, August 2, 2022

[FIXED] How to split string containing comma using span tag

Issue

There is a string containing tags separated by comma:

<div class="article" id="1">
    <span class="tags">Dog, Cat, Bird, Pig</span>
</div>
      
<div class="article" id="2">
    <span class="tags">Asia, Africa, Australia, Europe</span>
</div>
...

I would like to wrap each item using span tag within the tags class so that each of them can be individually styled like this

<span class="tags"><span>Dog</span><span>Cat</span>...</span>

Solution

$('.tags').html($('.tags').html().split(', ').map(function(el) {return '<span>' + el + '</span>'}))

Fiddle here



Answered By - keune
Answer Checked By - Mary Flores (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.