Skip to content

How to bring focus to an md-input? #472

@RoxKilly

Description

@RoxKilly

I'm having some trouble figuring out how to programmatically set focus on a <md-input> used as a search bar. My application hides the input (sets a CSS class with display:none) until the user clicks an icon indicating he'd like to perform a search. At this point, the CSS is changed (the class is removed) to reveal the input. I would like to automatically bring focus to it at this point. I've tried:

Template

<md-input [class.hidden]="searchOn" #searchbar>
<button (click)="onSearch(searchbar)">Start Search</button>

Component

onSearch(searchbar){
  searchbar.focus();  // <-- Exception. focus is not a method
  searchbar.focused = true; // <-- Exception. setting a property that has only a getter
}

How do you programmatically set focus on <md-input>?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions