If you're getting it from the database, you probably want to do this on the server side, and add those elements in your HTML; not with jQuery.
However, why use <span>
elements? The keyword in your title and your question I've seen is list - yet there is no use of (my favorite HTML elements!) <ul>
or <ol>
.
What server-side language are you using? I'll assume PHP... when you read the comma separated list out of the DB, turn it into a list, and then use CSS to add commas. You can do that with a background-image
of a comma, or with a CSS pseudo-element and the content
property.
Example
In a PHP server-side script:
<?php
$readFromTheDB = "foo, bar, mon, key, base, ball";
$list = $keywordsArray = array_map('trim', explode(',', $readFromTheDB));
echo "<ul>";
$oddEven = true;
foreach($list as $listitem) {
$class = ($oddEven?"odd":"even");
echo "<li class=\"".htmlentities($class)."\">".htmlentities($listitem)."</li>";
$oddEven = !$oddEven;
}
echo "<ul>";
?>
Then in your CSS file:
ul li {
background:url(path/to/images/comma.png) BOTTOM RIGHT NO-REPEAT;
display:inline;
}
ul li.even {
font-weight:bolder;
}