views:

70

answers:

3
$('fieldset.one label, fieldset.two label').each(function () {
    var className = $(this).text().trim().toLowerCase();
    $(this).addClass('default ' + className);
});

I'm adding classes dynamically to label elements, but when I try to style them, there is no effect. One of them has .sedan class added automatically, I see it in firebug, but the following doesn't have any effect:

.sedan {
  display:none !important;
}

HTML:

<fieldset class="one">
  <label>sedan</label>
  <label>suv</label>
  <label>truck</label>
</fieldset>

<fieldset class="two">
  <label>sedan</label>
  <label>suv</label>
  <label>truck</label>
</fieldset>
A: 

There's nothing wrong with the code you've posted here. I just tested it locally and it works just fine. Make sure your selectors are correct - and you can check to see whether the styles are being added with firebug.

Jonathan Sampson
There's a space after "default", and jQuery splits on spaces. OP's code should add two different classes to each element.
outis
OP's code *does* add two classes (given what was posted here).
Jonathan Sampson
A: 

Works for me: http://jsbin.com/amovo/edit

graphicdivine
A: 

You're using jQuery's trim method incorrectly it should be:

var className = $.trim($(this).text()).toLowerCase();
Richard M
Not sure why this was down-voted, the original code supplied will only work in recent versions of Firefox that actually implement the Javascript 1.8.1 `String.trim()` method.
Richard M
Ah, what was downvoted (by someone) is actually the solution! :)
Nimbuz
Nimbuz, your code worked as-is without this.
Jonathan Sampson
Yes, but it was failing in Chrome (mac) and Firefox (mac)
Nimbuz