tags:

views:

69

answers:

6

I'm receiving a comma delimited list of items from a database and placing them in a table cell, and I'd like to add alternating styles, so they are easily differentiated from one another.

ie,

foo, bar, mon, key, base, ball

such that the code looks something like this:

<td class="wide">foo, <span class="alt">bar</span>, mon, <span class="alt">key</span>, base, <span class="alt">ball</span></td>

So, I'd like to add a span class to the alternate values.

Is there an easy way to do that in jQuery?

+1  A: 

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;
}
LeguRi
Interesting solution. Probably semantically preferable, but seems like a bit of heavy lifting for a simple problem. Also, it doesn't seem like $oddEven does anything besides stay at 1?
yc
You're right! I missed a line of code; fixed it.
LeguRi
... but the underlying idea to add the elements and the class server-side isn't heavy; I think it's a much better solution than using JavaScript to do it.
LeguRi
I agree, using Javascript for this is probably making things a lot hard than they need to be. It makes so much more sense to do this on the server side, rather than using Javascript to modify the HTML afterwards.
Kibbee
@Kibee - Thanks! I appreciate the validation - I was wondering if my opinion was ass backwards ever since I got that commentless -1 :P
LeguRi
A: 

jQuery:

$('td.wide').find('span.alt').addClass('highlight');

where highlight should be defined as css class.

jAndy
please delight me for the downvote reason.
jAndy
It wasn't me, but I will say that it doesn't directly answer the question. the <span class="alt"></span> tags were what I'd wanted to add on an alternating basis using jquery; they don't yet exist, so adding a highlight class doesn't do anything, since the alt class was what i'd wanted to do the styling with.
yc
A: 

You're ambiguous as we don't know if you start with the <span> elements being present or if you only have the comma separated list as plain text, and the <span> elements are added by jQuery.

If you have the <span> elements before the jQuery

Using jQuery, you could use the :even selector.

$('td.wide span:even").addClass("alt");

If you don't have the <span> elements before the jQuery

(untested)

var list = $('td.wide').html();
var listAsArray = list.split(',');
var newListHtml = '';
for(var i=0; i<listAsArray.length; i++) {
    if(i%2==0) {
        newListHtml += '<span class="alt">';
    }
    newListHtml += listAsArray[i];
    if(i%2==0) {
        newListHtml += '</span>';
    }
}
$('td.wide').html(newListHtml);
LeguRi
Sorry, I don't start with anything but the comma delimited list, without any semantic markup.
yc
... and the second solution doesn't work?
LeguRi
+2  A: 

I would make the list into a ul, and then use the :nth-child selector to style each even li.

No, it wont work in every browser, but it is a far neater way than using span tags and javascript, and seeing as it is merely a minor visual aide (no meaning will be lost if the style is not displayed) it is less critical for it to work in older browsers.

For more info on :nth-child, see:
http://www.w3.org/TR/css3-selectors/#nth-child-pseudo
http://www.w3.org/Style/Examples/007/evenodd

cmrn
+1 for `<ul>`, and if I could there'd be another for `:nth-child`.
LeguRi
+1 and if the css `:nth-child` does not have sufficient browser support, you can always fall back on the jquery one.
jeroen
+1  A: 
function spanize(commaDelimitedString){
   var items = commaDelimitedString.split(',');
   var result = [];
   $.each(items, function(i){
       if(i%2 != 0)
           result.push('<span class="alt">' + this + '</span>');
       else
           result.push(this);
   });
   return result.join(',');
}

Nothing awesome, but works.

Matias
A: 

I believe you're looking for something like this:

jQuery

<script type="text/javascript">
  $(document).ready(function() {
    var data='foo, bar, mon, key, base, ball';

    // Add SPANs to all text parts
    $('#insertdata').html('<span>'+data.split(", ").join("</span>, <span>")+'</span>');

    // Add the highlighting to the odd spans
    $('#insertdata span:odd').addClass('alt');

    // Remove SPANs that are not needed
    $('#insertdata span:even').each(function() {
      $(this).replaceWith($(this).text());
    });
  });
</script>

CSS

<style type="text/css">
  .alt {
    font-weight: bold;
  }
</style>

HTML

<table>
  <tr><td id="insertdata"></td></tr>
</table>
Gert G