I have set up previous focus storage in my document. When the button is clicked, a function gets executed. The value 0 is transported as a variable from the button to the function. In the function the new value is set to the previous focussed element. But it doesn't work. Why not? The reason why I do it this way is because I have more buttons, that give different values but only have to use the same function: setNewValue
Main script:
window.prevFocus = $();
// Catch any bubbling focusin events (focus does not bubble)
$(document).on('focusin', ':input', function () {
// Test: Show the previous value
$("#debug").html(prevFocus.attr("id"));
// Save the previously clicked value for later
window.prevFocus = $(this);
});
Button script
$("#butSetVal0").on({
click: function(){
newVal = 0;
setNewValue(newVal);
}
});
set new value script:
function setNewValue(newVal){
prevFocus.val(newVal).trigger("input");
}
I fixed it already. The solution is to set the eventhandler of the button on 'mousedown'. The 'click' trigger detects two clicks. The second click interprets as the previous focus element, so the button itself.