Karax
How do I set the 'readonly' attribute for 'input'?
How do I set and get the position of the cursor in a 'textarea'?
How do I simulate the 'type=number' with a 'textarea'?
How do I set the 'readonly' attribute for 'input'?
input(readonly=toChecked(readonly))
How do I set and get the position of the cursor in a 'textarea'?
You will need to wrap setSelectionRange. Probably something like:
proc setSelectionRange(elem: InputElement; selectionStart, selectionEnd: int) {.importjs: "#.setSelectionRange(#, #)".}
proc setCursorPosition(elem: InputElement; position: int) =
elem.setSelectionRange(position, position)
How do I simulate the 'type=number' with a 'textarea'?
try
textarea:
proc onkeydown(e: Event; n: VNode) =
let val = (e.KeyboardEvent).keyCode
if val < 48 or val > 58:
# it is not a number key
e.preventDefault()
Thank you for your help again.
## Set the cursor of a 'textarea' to a position given in an 'input' field
# From javascript
proc setSelectionRange(elem: Element; selectionStart, selectionEnd: int) {.importjs: "#.setSelectionRange(#, #)".}
# For 'onkeyupenter = setCursorToPositionInTextarea' at
# 'input' for the position of the cursor
proc setCursorToPositionInTextarea(ev: Event, n: VNode) =
let pos = n.value.parseInt
let element = getElementById(kstring("textareaId"))
element.focus() # necessary
element.setSelectionRange(pos, pos)
## Simulate 'type=number" for a textarea
# 'onkeydown = onkeydownDigitsOnly' at textarea
proc onkeydownDigitsOnly(ev: Event; n: VNode) =
# Digits only, but editable
let keyCode = (ev.KeyboardEvent).keyCode
if (ev.KeyboardEvent).key == kstring("Alphanumeric") and (keyCode < 48 or keyCode > 58):
# it is not a digit key
ev.preventDefault()