How to make a function execute immediately after onclick in JavaScript?

Viewed 39

I want to change the caret position whenever the user clicks the mouse within a textbox. This is because I want to disable the possibility of the user changing the caret position; it has to stay constant and unchangeable.

But it seems that the function that onclick triggers is first executed about 0.3 seconds after the onclick actually occurs. There is a delay.

    input.onclick = function(){
        input.setSelectionRange(0, 0);
    }

input is the variable for the input field.

1 Answers

You might find more control with mouse events by also using the onmousedown event. onclick waits until the mouse button comes up. By the looks of it, could basically copy/paste another copy of this code, and just change onclick to onmousedown.

Related