htmx icon indicating copy to clipboard operation
htmx copied to clipboard

xh-on keyup event filter

Open 8483 opened this issue 1 year ago • 2 comments

This works:

<input 
        type="text" 
        name="description" 
        placeholder="task" 
        id="field-description"
        hx-on:keyup="alert('foo')"
    >

It would be great if we can specify the event...

<input 
        type="text" 
        name="description" 
        placeholder="task" 
        id="field-description"
        hx-on:keyup[key=='Enter']="alert('foo')"
    >

Without writing javascript handlers.

<input 
        type="text" 
        name="description" 
        placeholder="task" 
        id="field-description"
        hx-on:keyup="enterKeypressHandler(event);"
    >

<script>
    function enterKeypressHandler(event) {
        if (event.key !== "Enter") return;
        
        alert("foo");
    }
</script>

8483 avatar Dec 22 '23 16:12 8483

why not just do this?

<input 
        type="text" 
        name="description" 
        placeholder="task" 
        id="field-description"
        hx-on:keyup="if (key=='Enter') alert('foo')"
    >

dz4k avatar Dec 23 '23 16:12 dz4k

@dz4k Whoa, why didn't I think of that... Thank you for pointing it out, I made it work like this:

<input 
    type="text" 
    name="description" 
    placeholder="task" 
    id="field-description"
    hx-on:keyup="if (event.key=='Enter') focusElement('field-amount');"
>

<script>
    function focusElement(elementId) {
        document.getElementById(elementId).focus();
    }
</script>

8483 avatar Dec 24 '23 04:12 8483