Check a checkbox on keypress on a label

Tags: javascript css
By : luis
Source: Stackoverflow.com
Question!

I have the following on angular

<input type="checkbox" id="test"/>
<label for="test">Testing</label>

I'm trying to set a checkbox checked when i press enter on a label while using the tab and the focus is on the label

<input type="checkbox" id="test"/>
<label for="test" ng-click="setCheckBox($event)">Testing</label>

so far i tried using ng-click, ng-keydown or even onclick and nothing happens. i was hoping to achieve this using just css. thanks

By : luis


Answers
 $(document).ready(function(){
            $('#mycheckbox').keypress(function(event) {
                var keycode = (event.keyCode ? event.keyCode : event.which);

                if (keycode == 13) {
                    clickCheckBox(this);
                }   
                event.stopPropagation();
            });             
        });

        function clickCheckBox(box){

                var $box = $(box); 
                    $box.prop('checked',!$box.prop('checked')); 

        }

HTML

<input type='checkbox' id='mycheckbox'>dev</input>

http://jsfiddle.net/c2S5d/23/



look at these two forms

<label><input id="inputone" type="checkbox"/> Testing</label>

<input type="checkbox" id="inputtwo"/>
<label for="inputtwo">Testing 2</label>



I would use the following:

var startIndex = $('.start').index();
var endIndex = startIndex - 5;

for(var i = startIndex - 1; i >= endIndex; i--){
    $('.item').eq(i).addClass('good');
}

Updated to not add class 'good' the start index.

https://jsfiddle.net/n2723fmu/



This video can help you solving your question :)
By: admin