Set focus to dynamically created <input fields - use of ID, ref or name?

Asked
Active3 hr before
Viewed126 times

7 Answers

createdfieldsfocusdynamicallyinput
90%

I updated the stackblitz to focus using arrows keys. It's so simple than create a function,There are multiple ways to do it but this code is just using template, How do churches that have indefinitely suspended in-person services understand Hebrews 10:25? , How many iterations to reach the sequence?

I updated the stackblitz to focus using arrows keys. It's so simple than create a function

@ViewChildren('input') inputs: QueryList<ElementRef> //<--here declare our "inputs"
focusElement(index:number){
    const input=this.inputs.find((x,i)=>i==index)
    if (input)
      input.nativeElement.focus()
  }

And in the .html we ise keydown.arrowUp and keydown.arrowDown

<p *ngFor="let el of elements;let i=index">
   <input #input (keydown.arrowUp)="focusElement(i-1)" (keydown.arrowDown)="focusElement(i+1)">
</p>

Updated, as Victor comments below, there's a problem when you has no items. It's because I forget check if there are inputs. So, when subscribe to inputs.changes we need check it

this.inputs.changes.pipe(takeWhile(() => this.alive)).subscribe(() => {
   if (this.inputs.length) //<--add this "if"
      this.inputs.last.nativeElement.focus()
})
load more v
88%

<input type="datetime">,For more information about adding color to elements in HTML, see:,The form attribute lets you place an input anywhere in the document but have it included with a form elsewhere in the document.,<input type="submit">

<input type="button" name="button" value="Button" />
load more v
72%

@pcroc, You can define both, i.e. ElementRef and FormControl for same element and use the features.,Unfortunately that requires declaring an ElementRef for each form control. In the case of reactive forms with a dynamic form array, that won't work.,My thoughts: I'm surprised to even see the joint use the FormControl and ElementRef API's suggested as a permanent solution here since, per the Angular docs on ElementRef:,After reading this issue, I suspect that by relatively straightforward API they mean using ElementRef and calling focus() on the native element yourself.

[] bug report => search github
for a similar issue or PR before submitting
   [x] feature request[] support request => Please do not submit support request here, instead see https: //github.com/angular/angular/blob/master/CONTRIBUTING.md#question
load more v
65%

This example demonstrates how the menu button design pattern can be used to create a button that opens an actions menu. In this example, choosing an action from the menu will cause the chosen action to be displayed in the Last Action edit box. ,Action Menu Button Example Using aria-activedescendant: A button that opens a menu of actions or commands where focus in the menu is managed using aria-activedescendant.,Indicates that the menu is displayed and that activating the menu button closes the menu.,Navigation Menu Button: A button that opens a menu of items that behave as links.

75%

Create a dynamic action from the application page that invokes the action., Create a dynamic action from the application page that invokes the action. ,Change (change) - Fires when a control loses the input focus and its value has been modified since gaining focus. Supported User Interface Types: Desktop and jQuery Mobile Smartphone., Change (change) - Fires when a control loses the input focus and its value has been modified since gaining focus. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

For Dynamic Action implementation examples, go to the Learning Library at the following location, click the All Content tab and enter search criteria for Application Express (APEX) Product OBEs:

http://www.oracle.com/technetwork/tutorials/index.html

http: //www.oracle.com/technetwork/tutorials/index.html
load more v
40%

First, create a demo to add or remove input fields dynamically in React. Use the following article for more information.,Implement demo to add or remove input fields dynamically, Add or remove input fields dynamically with ReactJS,Add or remove input fields dynamically with ReactJS

2. Manage an input reference in the state variable

Now, let’s assume that we need to set the focus on the firstName when we add/remove input dynamically.

firstName
load more v
22%

Now let’s create a dialog programmatically, and change the dialog’s content dynamically,A programmatically created dialog with no content, with content added dynamically.,dojox.widget.Dialog - more flexible dialog allowing animations and different styles/theme support,Usage Programmatic instantiation Dialog via markup, and actionBar

require(["dijit/Dialog", "dojo/domReady!"], function(Dialog) {
   myDialog = new Dialog({
      title: "My Dialog",
      content: "Test content.",
      style: "width: 300px"
   });
});
load more v

Other "created-fields" queries related to "Set focus to dynamically created <input fields - use of ID, ref or name?"