Focus 'type in answer' in ankidroid does not work for numeric keyboard

Hello, in the deck type write response I added the following code

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const tags = document.body.getAttribute('data-tags');
        const inputField = document.querySelector('input[type="text"]');

        if (inputField) {
            if (tags && tags.includes('#TecladoNumerico')) {
                inputField.setAttribute('inputmode', 'numeric');
                inputField.setAttribute('pattern', '[0-9]*');
            } else {
                inputField.removeAttribute('inputmode');
                inputField.removeAttribute('pattern');
            }

            // Forzar teclado en dispositivos mĂłviles
            inputField.focus();
            if ('ontouchstart' in window) {
                setTimeout(() => {
                    const event = new Event('touchstart', { bubbles: true });
                    inputField.dispatchEvent(event);
                }, 100);
            }
        }
    });

</script>

<body data-tags="{{Tags}}">

So that the cards that have the label #TecladoNumerico when pressing the text box on my phone only the numeric keypad opens , so far everything is fine, but in Ankidroid in settings> advanced> Focus ‘type in answer’, if I activate this function, the keyboard does not open automatically, that is, with the previous code, despite having “Focus ‘type in answer’” activated, it does not open the keyboard, I have to manually tap the box to open it, do you think this is a bug or is there another way to do this?

You might want to try calling inputField.focus(); on a small timeout as well

I didn’t realize it, thank you very much, it worked :+1:

2 Likes