0

I have really strange problem with jQuery and label. It's weird because everything works when i for example chaning background of label, font-weight, but about :before selector fails.

Any ideas?

jquery:

$(".something").click( function (event) {
    $(".somediv label:before").css("background", "#000")

});

html:

     <ul>
        <li class="somediv">
            <label for="winter">winter</label>
            <input type="checkbox" class="something" id="winter" />
        </li>
    </ul>

1 Answers1

4

jQuery cannot be used to select psuedo elements as they do not exist within the DOM. To achieve what you need, you would have to use jQuery to change the parent label element. By class, for example:

$(".something").click( function (event) {
    $(".somediv label").addClass('active');
});

You can then pickup this class in CSS, and amend the psuedo element there:

.somediv label.active:before {
    background-color: #000;
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339