I want to programmatically generate a click event on a textbox when a user clicks on a button. I can see that the textbox gets the event but the focus doesn’t change to the textbox.
What am I doing wrong?
function handleClick(event)
{
let textbox = document.getElementById('text');
let rect = textbox.getBoundingClientRect();
let newEvent = new MouseEvent('click', {
clientX: rect.x + 5,
clientY: rect.y + 5,
x: rect.x + 5,
y: rect.y + 5,
pageX: rect.x + 5,
pageY: rect.y + 5
});
textbox.dispatchEvent(newEvent);
}
<input type="text" id="text" onclick="console.log('clicked')" />
<button onclick="handleClick(event)">Click</button>