Setting focus on a specific element within a component nested within a Mat Dialog - a comprehensive guide

Is there a way to target a submit button within the

<wknw-dialog-actions></wknw-dialog-actions>
component when using the autoFocus property in a MatDialog?

To provide better clarity, here is the structure of a dialog component:

contact-group-edit.component.ts (dialog opened via matDialog.open...)

<form [formGroup]="contactGroupForm" [appConnectForm]="contactGroup$ | async">
  <wknw-dialog-title (closeEvent)="close()"> {{title}} </wknw-dialog-title>
  <wknw-dialog-content>
    <mat-form-field>
      <mat-label i18n>Name</mat-label>
      <input matInput formControlName="name" />
    </mat-form-field>
  </wknw-dialog-content>

  <wknw-dialog-actions <-- my submit button is inside this component -->
    (confirmEvent)="save()"
    (closeEvent)="close()"
    [dialogType]="dialogType"
    [submitDisabled]="!contactGroupForm.valid"
  ></wknw-dialog-actions>
</form>

wknw-dialog-actions.component.ts

<div #ref><ng-content></ng-content></div>
<ng-container *ngIf="!ref.hasChildNodes()">
    <button mat-raised-button mat-dialog-close *ngIf="showButtonCancel" (click)="closeAction()" i18n>
        Cancel
    </button>


// button that needs to be focused below


    <button mat-raised-button [disabled]="submitDisabled" [color]="confirmButtonColor" (click)="confirmAction()" class="btn-confirm-action">
        {{confirmButtonTitle}}
    </button>
</ng-container>

I have attempted setting an id for the component itself, like:

<wknw-dialog-actions id="actions"...
and attempting to target the submit button as follows:

const dialogConfig: MatDialogConfig = {
    autoFocus: '#actions .btn-confirm-action'
}

However, this approach was unsuccessful.

Answer №1

const button = document.getElementById("button_id");
button.focus();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Change the outcome of each element in a JavaScript forEach loop by incorporating values from a

I'm struggling to adjust the output of my forEach loop using values from another array. Despite my efforts, I haven't been able to make it work. const BbDescriptionDictionary = ['AAA' , 'BBB', 'CCC',] const boardBa ...

Unusual behavior displayed by Android webview

In landscape orientation, I have a horizontal layout with buttons on the left and a webview on the right like this: Everything works fine in the WebView until an absolutely or fixedly positioned element is added to it. For example, this element: <div ...

What is the reason that elements with % properties totaling 100% do not align next to each other?

I've been attempting to arrange two blocks side by side within another block, but they just don't seem to fit together smoothly. What could be causing this issue? .container {height: 200px; width: 400px; background:darkgrey;} .left {height: 100% ...

Utilize HTML5, CSS, and Responsive Web Design to place four boxes into a div container

Is there a way to arrange four boxes inside a div so that they are positioned at the corners of the top-left, top-right, bottom-left, and bottom-right? And when you click on the box in the middle, a window opens with text. I'm looking for something s ...

CSS-powered animation of a div's movement

Is there a way to make the "icon" div move dynamically when the text is long? Does anyone have any suggestions on how to achieve this? I appreciate any help or advice. .tit { display: table; } .icon { border-top: 5px solid currentcolor; border-ri ...

Enhancing the appearance of the final column within static columns of a data table

I am facing an issue where I need to add a shadow next to a set of fixed columns in order to indicate that there is content beneath them. These fixed columns are marked with the class dtfc-fixed-left which is assigned by the FixedColumns library within Dat ...

The default value is not appearing in the Select Box when using [(ngModel)] in Angular 2

I have encountered an issue with the code snippet below. When I use variable binding "[(ngModel)]", the default option "Title*" is not visible. However, if I remove it, the first option is shown as selected by default. <select name="title" id="title ...

Apply a border around every digit within a division

Is there a way to add a border to each individual number within a div? I already know how to add a border to a div as a whole, but I specifically want each number inside the div to be bordered. For example: <div id="borders">12345</div> The ...

The functionality of box-sizing: border-box does not seem to be working properly when used in

I am currently utilizing the Cleanslate tool to reset all CSS attributes on my widget container along with its children. I have also attempted to apply 'box-sizing: border-box' to all elements, but I am encountering unexpected behavior. Specific ...

Apply CSS styling (or class) to each element of a React array of objects within a Component

One issue I'm facing involves adding specific properties to every object in an array based on another value within that same object. One such property is the background color. To illustrate, consider an array of objects: let myObj = { name: "myO ...

Issue where CSS modal does not appear when clicked after being toggled

I've been working on a custom modal design that I want to expand to fill the entire width and height of the screen, similar to how a Bootstrap modal functions. The goal is to have a container act as a background with another inner div for content How ...

Where can I find information about making Angular Material 2 responsive?

Just diving into Angular Material 2! Anyone have tips on creating a responsive theme? ...

Sorting and saving and identifying the parent-child connections among elements within an array

I am currently developing an Angular application and here is the structure of my data: data= [ { "id": 2, "name": "Jamie", "objectId": 200, "parentId": null, "children": [ { "id": 98, ...

Harness the power of multiple backgrounds with just one CSS style!

I'm exploring a way to incorporate multiple images as backgrounds into my website design. For instance, having a car image on the index page and a notepad image on the about me page. My attempt involved using the following code: body { back ...

When accessing a page protected by Angular AuthGuard, users may be redirected but could experience the issue of the page

The Routes are structured as follows: const routes: Routes = [ { path: '', component: NewsfeedComponent, children: [ { path: ':id/edit', component: NewsfeedEditComponent, canActivate: [AdminG ...

Transfer data to clipboard

Is there a way to copy information via href to the clipboard? I'm looking to achieve something like this; <?php print "Number: <a href=\"" . $number . ">" . $number . "</a>"; ?> When someone clicks the link, the information ...

Cyber Platform

I recently encountered a challenge while working on my web project. What are some areas that can be improved? import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import {map} from 'rxjs/op ...

Encountering an Angular 9 Ivy issue when using the <mat-form-field> with multiple mat-hints

After migrating to angular9 Ivy, I encountered an issue with multiple mat-hints in a single component. Before the update, my code looked like this: <div class="example-container"> <mat-form-field hintLabel="Max 10 characters" ...

"Maximizing Bootstrap Functionality with Angular 2: Step-by

I'm experiencing an issue with Angular2, Bootstrap 3.3.7, and Bootstrap Plugins. I have imported all the necessary stylesheets and JavaScript files in the 'index.html' file. Below is the snippet from the index.html: <!-- Bootstrap & ...