I want to set focus to a form element. With jQuery this is so easy, just $('selector').focus
.
I have created a simple <input ref="xxx" id="xxx" type="text">
and in my .ts file I add a property and use it here:
attached() {
this.xxx.focus()
}
Nothing happens. Beginning to feel that some easy things are getting difficult, which I'm sure never was the point.
(the original use case was to set focus to a form element inside a Bootstrap collapse, when the collapse is shown, like this:
// set focus to the first element when the add-cell collapse is shown
$('#collapsedAddTask').on('shown.bs.collapse', function () {
$('#AddTaskTitle').focus()
})
That works for my basic HTML (not single page app), but not with Aurelia. What's the Aurelia way?
Update Using the answer from Miroslav Popovic below, and in the comments, I got it to work like so (remember, this is a Bootstrap collapse component):
<!-- the heading with an A that toggles visibility -->
<div class="panel-heading">
<a href="#collapsedAddTask" data-toggle="collapse" id="addTaskHeader" click.delegate="addTaskShown()">Add task</a>
</div>
<!-- the body, which is initially hidden -->
<div class="panel-body">
<div class="collapse" id="collapsedAddCell">
<input type="text" focus.bind="addTaskFocused">
<!-- more controls ... -->
</div>
</div>
$('#collapsedAddTask').on('shown.bs.collapse', function () { .... })
or is there an Aurelia way? – Foveola