Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to make this submit button to act like a enter button when pressed ?

chercm
Mega Sage

how to make this submit button to act like a enter button when pressed ? 

 

<input
autofocus="autofocus"
class="input custom-input-height barcode-input"
ng-model="barCode"
placeholder="Please tap here before scanning barcode..."
type="text"
required
id="barcodeInput"
ng-keypress="handleKeyPress($event, 'barcode')">
<button
class="btn btn-primary checkin"
ng-click="handleCheckIn()"
ng-disabled="!barCode || runningRequests > 0"
type="submit">
Check-In
</button>
</div>
</form>

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@chercm 

try this

<input
    autofocus="autofocus"
    class="input custom-input-height barcode-input"
    ng-model="barCode"
    placeholder="Please tap here before scanning barcode..."
    type="text"
    required
    id="barcodeInput"
    ng-keypress="handleKeyPress($event, 'barcode')"
    onkeypress="if(event.keyCode == 13) document.getElementById('checkInButton').click();">
<button
    class="btn btn-primary checkin"
    ng-click="handleCheckIn()"
    ng-disabled="!barCode || runningRequests > 0"
    type="submit"
    id="checkInButton">
    Check-In
</button>

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar  when i click on the onscreen keyboard it works but when i press on the check in button , it is not sending as enter button . what i was trying to archive is that the autofocus is not working on my widget. 

 

when i use the external barcode reader and the onscreen keyboard it will cursor will stay at the barcode text box but not when i press on the check in button 

 

here is the code for the check in : 

 

 $scope.handleCheckIn = function (event) {
        if (event) {
            event.preventDefault(); // Prevent default form submission
        }
 
        console.log("Barcode Check-In triggered with:", $scope.barCode);
 
        // Validate input
        if (!$scope.barCode || $scope.barCode.trim() === '') {
            $scope.scanError = 'Please scan a barcode.';
            setInitialFocus('barcodeInput'); // Refocus on the barcode input
            return;
        }
 
        $scope.runningRequests++;
        $scope.scanError = null;
 
        // Simulate API call for barcode check-in
        c.server
            .get({
                barCode: $scope.barCode,
                locationId: $scope.locationId
            })
            .then(function (response) {
                if (response.data.error) {
                    $scope.scanError = response.data.error;
                } else {
                    console.log("Check-In Successful:", response);
 
                    if (response.data.interactionNumber) {
                        $scope.interactionUniqueValues.unshift({
                            interactionNumber: response.data.interactionNumber,
                            message: response.data.queuePositionMessage || 'You have been added to the queue.'
                        });
                    }
 
                    // Temporarily block the modal
                    // Uncomment the following block to re-enable the modal
                    /*
                    if (response.data.queuePositionMessage) {
                        $scope.data.queuePositionMessage = response.data.queuePositionMessage;
                        $('#queuePositionModal').modal('show');
 
                        $timeout(function () {
                            $('#queuePositionModal').modal('hide');
                        }, 5000); // Close modal after 5 seconds
                    }
                    */
                }
            })
            .finally(function () {
                $scope.runningRequests--;
                clearFields();
 
                // Refocus on the barcode input box
                $timeout(function () {
                const checkInButton = document.getElementById('barcodeCheckInButton');
                if (checkInButton) {
                    checkInButton.blur(); // Remove focus from the button
                }
 
                const barcodeInput = document.getElementById('barcodeInput');
                if (barcodeInput) {
                    barcodeInput.focus(); // Place cursor in the input box
                    barcodeInput.setSelectionRange(barcodeInput.value.length, barcodeInput.value.length); // Set cursor at the end
                }
            }, 0); // Immediate timeout for better UX
 
            clearAlerts();
        });
};
 

// Comprehensive Focus Management
function setInitialFocus(inputId = 'barcodeInput') {
$timeout(function () {
var barcodeInput = document.getElementById(inputId); // Get the input element
if (barcodeInput) {
barcodeInput.focus(); // Focus the input field
barcodeInput.click(); // Trigger a click (important for iOS virtual keyboard)

// Ensure focus reliability (especially on iOS)
setTimeout(function () {
barcodeInput.focus(); // Reapply focus
barcodeInput.setSelectionRange(barcodeInput.value.length, barcodeInput.value.length); // Set cursor at the end
}, 50); // Slight delay for stability
}
}, 0); // Execute focus logic immediately
}

 

image.png