I want to select the text of the input element on focus. I tried with bind:this={ref} and then ref.select(), but this seems only to work when i remove the bind:value from the input element. Why? and how to solve?
Many thanks!
<script lang="ts">
import { evaluate } from 'mathjs';
export let value: string | number = 0;
let ref;
function handleFocus() {
value = value?.toString().replace('.', '').replace(',', '.');
ref.select();
}
function handleBlur() {
value = parseFloat(evaluate(value?.toString())).toLocaleString('be-NL', {
maximumFractionDigits: 2,
minimumFractionDigits: 2
});
}
</script>
<input
class="text-right"
autocomplete="off"
type="text"
bind:value
bind:this={ref}
on:focus={handleFocus}
on:blur={handleBlur}
/>