codeChris
codeChris

Reputation: 885

Custom checkbox with hidden inputs not selecting properly. Javascript needs adjusting

I am using custom checkboxes with the input hidden wrapped in a span; I have written a script to add the class for checked, but it doesn't actually mark the inputs as checked. What am I doing wrong here?

UPDATE: So the checkbox checks now! Hooray. A new problem is that hiding the visibility on the checkbox, also hides the new class when checked on the area of the checkbox... if the label is clicked, then the checkbox shows as checked, but if the checkbox image itself is clicked, nothing changes...

HTML

<fieldset>
  <label class="sublabel" title="Insights & Planning" for="checkbox-insights-and-planning">Insights & Planning</label>

  <span class="open-checkbox">
     <input id="checkbox-insights-and-planning" class="key-areas-checkbox" name="key-areas-of-interest" type="checkbox" />
  </span>
</fieldset>

CSS

.open-checkbox {
    background-position: -4px -48px;
    background-image: url(../images/adcolor-sprite.png);
    background-repeat: no-repeat;
    cursor: pointer;
    display: block;
    float: right;
    height: 25px;
    margin-top: 10px;
    width: 25px;
}

.checked {
    background-position: -4px -12px;
    background-image: url(../images/adcolor-sprite.png);
    background-repeat: no-repeat;
    cursor: pointer;
    display: block;
    float: right;
    height: 25px;
    margin-top: 10px;
    width: 25px;
}

input[type=checkbox] {
    visibility: hidden;
}

#key-areas-inputs label.sublabel {
    display: block;
    float: left;
    height: 44px;
    padding: 0 0 0 10px;
    width: 300px;
}
#key-areas-inputs input.key-areas-checkbox {
    float: right;
    display: block;
    clear: none;
    height: 22px;
    width: 22px;
    margin-top: 18px;
    margin-right: 4px;
    margin-bottom: 0px;
    margin-left: 0px;
}
#key-areas-label {
    font-family: "Futura W01 Bold", Arial, Helvetica, sans-serif;
    font-size: 16px;
    color: #df007c;
    display: block;
    clear: left;
    float: left;
    width: 348px;
    margin-top: 50px;
    margin-right: 0px;
    margin-bottom: 0px;
    margin-left: 0px;
    padding-top: 0px;
    padding-right: 30px;
    padding-bottom: 0px;
    padding-left: 0px;
    text-align: right;
}

JS

// check checked boxes on load     
    $(".key-areas-checkbox:checked").each(function(){
        $(this).next().addClass('checked');
    });

    // add class and checked attribute to filter inputs
    $('.open-checkbox').click(function () {
        console.log('click');
        var $this = $(this);
        var $input = $this.find('input');

        $this.toggleClass('checked', $input.is(':checked'));
        return;

        if($input.prop('checked')){
            $this.removeClass('checked');
        } else {
            $this.addClass('checked');
        }
    });

Upvotes: 0

Views: 297

Answers (3)

codeChris
codeChris

Reputation: 885

I figured out what the issue was. The JS was fine. The fix was to nest the inputs inside of the label and wrap inside of a div. This gave the desired effect.

Upvotes: 0

Alex Art.
Alex Art.

Reputation: 8781

to check the checkbox add:

 $input.attr('checked','checked');

to uncheck:

$input.removeAttr('checked');

Upvotes: 2

keirog
keirog

Reputation: 2178

Your for and ids don't match. Change your id to "checkbox-insights-and-planning" (was missing an 's') and it should work.

Also make sure your image path is correct (we can't test that with your example code as it's a relative path; I just tried it with a background-color and it worked).

Upvotes: 1

Related Questions