Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why blur and focus doesn't work on Safari?

I have simple button like that:

<button class="emoji-button-container" onblur="APP.hideEmojiContainer()" onclick="APP.toggleEmojiContainer()">

On Chrome both event works perfectly.

On Safari onclick event works without any problem, but onblur event doesn't get triggered. Also element.blur() function doesn't trigger onblur event. I need it to work on Safari just like on Chrome, so what can I do? what's problem here?

like image 945
O. Shekriladze Avatar asked Dec 18 '22 13:12

O. Shekriladze


1 Answers

On Safari, buttons may not be focused on click (and because they do not focus, they do not trigger blur)

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus

You can focus the button using javascript though

<!DOCTYPE html>
<html lang="en">
    <head>
        <script type="text/javascript">
            function clickFunc(event) {
                console.log(event, 'click');
                event.target.focus();// Add me
            };

            function blurFunc(event) {
                console.log(event, 'blur');
            };
        </script>
    </head>

    <body>
        <button class="emoji-button-container" onblur="blurFunc(event)" onclick="clickFunc(event)"></button>
    </body>
</html>
like image 133
Mark Thompson Avatar answered Dec 31 '22 03:12

Mark Thompson