views:

47

answers:

1
<label>Hatchback    <input type="checkbox" name="" /></label>
<label>Bike  <input type="checkbox" name="" /></label>
<label>Sedan     <input type="checkbox" name="" /></label>
<label>Scooter   <input type="checkbox" name="" /></label>
<label>Coupe     <input type="checkbox" name="" /></label>
<label>SUV   <input type="checkbox" name="" /></label>

I'd like to grab the text in labels and apply them as class names.

This doesn't work:

var classname = $("fieldset.visual label").text();
$("fieldset.visual label").addClass(classname);
+4  A: 

Use each to apply just the text to that specific label as a class.

$('fieldset.visual label').each( function() {
    var className = $(this).text();
    $(this).addClass(className);
});
tvanfosson
Almost perfect, just one thing: possible to lowercase all and remove spaces if found? Many thanks!
Nimbuz
Or maybe just use the first word, if thats easier to code?
Nimbuz
`className.toLowerCase()` and `className.replace(/ /g, '')` to get rid of spaces.
Matt Ball
@Bears: Thanks heaps! :)
Nimbuz