views:

146

answers:

4
var os = $.client.os; // mac
var browser = $.client.browser; // firefox
var browserversion = $.client.browserversion; // 3
$('#root').addClass( os + browser + browserversion );

.. results in <div id="root" class="macfirefox3">. How do I add spaces between them?

+5  A: 
$('#root').addClass( os + " " + browser + " " + browserversion );
Falcon
erm..I knew this already, no neat way to do it?
Nimbuz
@Nimbuz: this is the neat way.
Andy E
@Nimbuz: What would make this *neat*, in your opinion? ... OK. I thought so. :)
Marko Dumic
@Marko : see the accepted answer. :)
Nimbuz
A: 

You need to concatenate spaces too, Falcon gave you an example already

Wai Wong
+3  A: 

Another variation of the same:

$('#root').addClass([
    $.client.os,
    $.client.browser,
    $.client.browserversion
].join(' '));
Marko Dumic
Perfect, thanks! :)
Nimbuz
A: 

Considering jQuery's philosophy, the addClass method should really accept an array or multiple strings as arguments without you having to concatenate them.

So here's my 2 cent plugin called addClasses, basically a wrapper around what Marko did.

jQuery.fn.addClasses = function() {
    var classes = [];
    $.each(arguments, function(index, name) {
        classes.push(name);
    });
    this.addClass(classes.join(' '));
};

Allows you to send any number of class names as string arguments.

$('div').addClasses('message', 'greeting', 'w00t');
Anurag