tags:

views:

32

answers:

1

I am working on a small bit of script that will allow for easy and flexible creation of buttons.

My buttons are made of three parts, a left, a right and a streched middle piece. There are three different sizes and three different colours of each.

I have written the code that resizes the button to fit the text. Now I am looking to add the ability to easily set the colour and size.

I was thinking of using three classes something like <div class="button blue big"> My script already loops through the button classes using each. Now I need to find out what other classes are set and adjust the pictures and dimentions used to fit.

So how can I find out what other class selectors an element has?

Also I am fairly new to jquery/javascript so I am wondering if storing the design as class selectors is the best way.

+5  A: 

To get all classes you can do this:

$('div.button').attr('class').replace(/\s+/,' ').split(' ');

will return

['button','blue','big']

Update:

Or to check if an element has a specific class:

$('div.button').hasClass('blue');
// or
$('div.button').hasClass('big');
jigfox
Awesome thankyou!
Jigs
Make sure your code does not trip on the class attribute accidentally containing two spaces: `"foo__bar".split('_') == ['foo', '', 'bar']`
Tgr
@Tgr: thanks and done
jigfox