<form> <input type="checkbox" name="left_block" value="1111"> <input type="checkbox" name="left_block" value="2222"> <input type="checkbox" name="left_block" value="3333"> <input type="checkbox" name="left_block" value="4444"> <br> <input type="text" name="txt1" id="txt1"> <input type="text" name="txt2" id="txt2"> <br> </form> <script> (function(bind) { var form = document.forms[0], ult = form.txt1, penult = form.txt2, history = []; bind.call(form, 'click', function voodoo(e) { var node = e.target || e.srcElement, i, value = node.value, entries; if(node.name != 'left_block') { return; } if(node.checked) { history.push(value); } else { i = history.length; while(i--) { if(history[i] == value) { history.splice(i, 1); } } } entries = history.slice(-2) ult.value = entries.shift() || ''; penult.value = entries.shift() || ''; }); })(document.addEventListener ? function(type, fn, bubble) { this.addEventListener(type, fn, !!bubble); } : function(type, fn) { this.attachEvent('on' + type, fn); }); </script>