In my HTML file is use onscan.js. My problem is that if i select an input field the scanner writes to that input (cardnameinput) and (cardidinput) instead of only (cardidinput). If i deselect everything the scanner inputs in the right field. No mater where the focus is i only want to scan to (cardidinput)
$(document).ready(function () {
onScan.attachTo(document, {
onScan: function (sCode, oEvent) {
// Save current focus
var currentFocus = document.activeElement;
// Temporarily disable all input fields except #cardidinput
$("input").not("#cardidinput").prop("disabled", true);
// Set focus to #cardidinput and insert scanned code
$("#cardidinput").focus().val(sCode);
console.log("Card successfully scanned " + sCode);
// Restore original focus and re-enable all input fields
if (currentFocus && currentFocus !== document.body) {
currentFocus.focus();
}
$("input").not("#cardidinput").prop("disabled", false);
},
onScanError: function (oEvent) {
console.log("Error reading card");
},
suffixKeyCodes: [13], // Enter key ends the scan
avgTimeByChar: 45,
minLength: 4
});
});
function simulate(){
onScan.simulate(document, '1234567890123');
}
button{
cursor: pointer;
font-size: 2rem;
}
input{
font-size: 2rem;
margin-bottom: 1em;
}
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://unpkg.com/[email protected]/onscan.js"></script>
<input type="text" id="cardnameinput" class="k-textbox" style="width: 100%;" >
<input type="text" id="cardidinput" class="k-textbox k-state-disabled" style="width: 100%;" >
<button type="button" onclick="simulate()">simulate</button>
13