JQuery and shortcuts

In addition to my previous question, I have a jQuery event that I want to fire when the checkbox and the shortcut itself are clicked.

jsFiddle here.

My problem is that when I click on the label, it does not start the function. The checkbox works fine, as you can see.

Can I add anything else? Or that?

Thank:)


EDIT: code from jsFiddle link

HTML

<div id="scrollwrap">
    <div>
       <input value="1"  type="checkbox" name="salgsvilkar" id="checkbox2"  style="float:left;"/>
        <label for="checkbox2" class="akslabel">Salgs og leveringsvilkår er lest og akseptert</label>
    </div>
</div>

JQuery

$(function() {
    //checkbox
    $("#checkbox2, .akslabel").click(function() {
        $("#scrollwrap").toggleClass('highlight');
    });
});
+3
source share
2 answers

You can change this a bit to be safe:

$(function() {
  $("#checkbox2").change(function(){
    $("#scrollwrap").toggleClass('highlight', this.checked);
  });
}); 

You can check it out here . The advantage here is that you can add .change()to the end so that the match matches if it was originally checked, for example:

$(function() {
  $("#checkbox2").change(function(){
    $("#scrollwrap").toggleClass('highlight', this.checked);
  }).change();
}); 

. , .

+4

:

$(function() {
   //checkbox
   $("#checkbox2").change(function(){
    $("#scrollwrap").toggleClass('highlight');
  });
}); 

, , click(), , , , !

+2

Source: https://habr.com/ru/post/1770295/


All Articles