Skip to content
Related Articles

Related Articles

Angular PrimeNG Focus Trap Input with tabindex -1

View Discussion
Improve Article
Save Article
  • Last Updated : 14 Aug, 2022
View Discussion
Improve Article
Save Article

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use the Focus Trap Input with tabindex -1 in Angular PrimeNG. We will also learn about the various properties and their syntaxes used in the code example.

Angular PrimeNG Focus Trap is used to maintain focus on certain DOM elements while we tab with the tab key. Input with tabindex -1 means that sequential keyboard navigation cannot get to the element, but JavaScript or mouse clicking can focus it visually.

Angular PrimeNG Focus Trap Input with tabindex -1 properties:

  • tabindex: It describes an element’s tab order while navigating with the “tab” button.

Syntax:

<input type="text" 
    size="..." 
    pInputText 
    tabindex="..." 
/>

Creating Angular application & module  installation:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

 

To run the above file run the below command:

ng serve --save

Example 1: Below is the example that illustrates the use of Angular PrimeNG Focus Trap Input with tabindex -1.

app.component.html




<div pFocusTrap class="card">
  <h2 style="color: green">GeeksforGeeks</h2>
  <h5>Angular PrimeNG Input with tabindex -1</h5>
  
  <input
    type="text"
    size="25"
    pInputText
    placeholder="This input field will get focused" />
  
  <input
    type="text"
    size="29"
    pInputText
    placeholder="This input field will not get focused"
    tabindex="-1" />
  
  <input
    type="text"
    size="25"
    pInputText
    placeholder="This input field will get focused" />
</div>

app.component.ts




import { Component } from '@angular/core';
  
@Component({
     selector: 'app-root',
     templateUrl: './app.component.html',
})
  
export class AppComponent {}

app.module.ts




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { InputTextModule } from 'primeng/inputtext';
import { EditorModule } from 'primeng/editor';
  
@NgModule({
    imports: [
       BrowserModule,
       InputTextModule,
       EditorModule,
       HttpClientModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule {}

Output:

 

Example 2: Below is another example that illustrates the use of Angular PrimeNG Focus Trap Input with tabindex 1, tabindex 2, tabindex -1, and tabindex 3.

app.component.html




<div pFocusTrap class="card">
  <h2 style="color: green">GeeksforGeeks</h2>
  <h5>Angular PrimeNG Input with tabindex -1</h5>
  
  <div class="row">
    <div class="col">
      <input
        type="text"
        size="10"
        pInputText
        placeholder="tabindex=1"
        tabindex="1" />
    </div>
  </div>
    
  <div class="row">
    <div class="col">
      <input
        type="text"
        size="10"
        pInputText
        placeholder="tabindex=-1"
        tabindex="-1" />
    </div>
  </div>
    
  <div class="row">
    <div class="col">
      <input
        type="text"
        size="10"
        pInputText
        placeholder="tabindex=3"
        tabindex="3" />
    </div>
  </div>
    
  <div class="row">
    <div class="col">
      <input
        type="text"
        size="10"
        pInputText
        placeholder="tabindex=2"
        tabindex="2" />
    </div>
  </div>
</div>

app.component.ts




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {}

app.module.ts




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { InputTextModule } from 'primeng/inputtext';
import { EditorModule } from 'primeng/editor';
  
@NgModule({
    imports: [
       BrowserModule,
       InputTextModule,
       EditorModule,
       HttpClientModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule {}

Output:

 

Reference: https://primefaces.org/primeng/focustrap 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!