A
A
Alexander Interesting2021-06-19 21:40:01
JavaScript
Alexander Interesting, 2021-06-19 21:40:01

How to check if TAB is pressed in textarea?

I have a textarea and two buttons, pressing the TAB button selects the next object in , rather than generating (4) spaces in the textarea. Can you tell me how to do it manually? <div>Объекты внутри этого "дива" выделяются</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Interesting, 2021-06-19
@Swimergg

I figured it out myself:

document.getElementById('OBJECT ID').addEventListener('keydown', function(clickevent) {
        if (clickevent.key=='Tab') {
            var begin = this.selectionStart;
            var e = this.selectionEnd;
            this.value = this.value.substring(0, begin)+"\t"+this.value.substring(e);
            this.selectionStart = this.selectionEnd = begin+1;
            clickevent.preventDefault();
        }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question