0

I'm using this for custom radio buttons and checkboxes, but I need to change the :before element's background on click. Is it possible?

HTML

<div class="checkbox">
<input type="checkbox" value="1" name="test">
</div>

CSS (its .less)

.checkbox {
    display: inline-block;
    margin: 0 4px 0 0;
    cursor: pointer;
    text-align: left;
    padding: 0px;

    input { display: none; }

    &:before {
        background: url(/img/checkboxes.png) no-repeat;
        content: "";
        float: left;
        height: 18px;
        width: 19px;
        margin: 0;
    }
}
Stefanie
  • 467
  • 1
  • 7
  • 17

1 Answers1

2

You can't select pseudo elements with jQuery, I'd say the cleanest solution is to manipulate the class of the element with jQuery:

.checkbox {
    display: inline-block;
    margin: 0 4px 0 0;
    cursor: pointer;
    text-align: left;
    padding: 0px;

    input { display: none; }

    &:before {
        background: url(/img/checkboxes.png) no-repeat;
        content: "";
        float: left;
        height: 18px;
        width: 19px;
        margin: 0;
    }

    &.someclass:before {
      background-position:0 -55px;
    }
}

Then:

$('.checkbox').addClass('someclass');
Dominic
  • 62,658
  • 20
  • 139
  • 163