0

Having a hard time attaching a click event. I just want to do a few calculations when user checks a checkbox. When a checkbox is checked the span's class becomes 'checked' & 'unchecked' when unchecked. The input checkbox is not displayed & also its attribute 'checked' is not applied, when checked, making it even more difficult.

HTML:

<div class="checkboxBtn" style="border:1px solid blue;">
    <span class="cheked" style="width:20px;height:20px;border:1px solid red;" 
     onclick="CheckSelected();">
    </span>
    <input id="23" type="checkbox" style="border:1px solid green;"></input>
    <label>Compare Now</label>
</div>

None of these worked:

JS:

$(document).ready(function () {
        $(".cheked").click(function () {
            alert(".cheked");
        })
        $(".uncheked").click(function () {
            alert(".uncheked");
        })
    })
function CheckSelected() {
        alert("");
        return false;
    }

enter image description here

Is this not working because, there might be already a click event to show the tick image. If there is any already, how can I do my task, without disturbing the existing functionality.

3

3 Answers 3

2

Try this - Apply class to Checkbox and bind change event.

<div class="checkboxBtn" style="border:1px solid blue;">
    <span class="cheked" style="width:20px;height:20px;border:1px solid red;" 
     onclick="CheckSelected();">
    </span>
    <input id="23" class="chkBx" type="checkbox" style="border:1px solid green;"></input>
    <label>Compare Now</label>
</div>

$(document).ready(function() {    


    $('.chkBx').change(function() {
        if($(this).is(":checked")) {
         $(this).closest('span').removeAttr('class');
         $(this).closest('span').addClass('checked');
        }
        else{
         $(this).closest('span').removeAttr('class');
         $(this).closest('span').addClass('unchecked');
        }                
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Just complementing your good answer. If wanted to have only two states, instead of three (undefined, checked and unchecked), it's possible to use only the class 'cheked'. In this case, on the CSS should be used ".checkboxBtn span.cheked" and ".chedkboxBtn span:not(.cheked)"
1

This is because the '.click' handler will attach event to all of the matched element specified by selector at the time of invocation but not to the future element. i.e if an element with class name 'cheked' added later by JavaScript, the click handler will not be attached to that obviously.
Therefore you should add a dummy static class to attach handler. Note that event attached to the checkedbox is fired after changing the checkbox status.

Comments

1

Here is the updated working code.

<div class="checkboxBtn">
    <span id="23_span" class="unchecked"></span>
    <input type="checkbox" id="23"></input>
    <label for="23">Compare Now</label>
</div>


<script>
$(document).ready(function () {
    $("#23").click(function () {
        CheckSelected();
    })
})

function CheckSelected() {
    if(document.getElementById('23').checked) {
        $("#23_span").removeClass("unchecked").addClass("checked");
        alert("checked");
    }
    else {
        $("#23_span").removeClass("checked").addClass("unchecked");
        alert("unchecked");
    }
}
</script>

Now you can provide CSS to your span element according to the requirement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.