Execute a method in an HTML element without using the click event

0

Good night, I have a radio button on a form, where it is only called when it meets the condition of an * ngIf, however, when ngIf is accepted and this element is called, I need a method to execute without you need to click any button. Here are my code snippets:

<mat-radio-button name="G01" *ngIf="getClass0()" class="margem" value=0 checked>0</mat-radio-button>

    Desabilitar(element){
var radio = document.getElementById(element) as HTMLInputElement;
if(radio.style.display == 'none'){
  radio.style.display = 'table-cell';
}else radio.style.display = 'none';

}

I need this method to be called as soon as this radio button appears on the screen.

    
asked by anonymous 15.11.2018 / 21:54

1 answer

0

You should not use window.onload, as some elements may be loaded asynchronously, so they may not yet be found.

To have a greater warranty use:

If you're used to jquery:

$(document).ready(function(){
  Desabilitar(...)
});

If you want something without dependency, use:

document.addEventListener('DOMContentLoaded', function() {
   Desabilitar(...)
}, false);

Know that the examples above will almost certainly work, but they may still not work in some browsers, so this example below is guaranteed in any browser:

setTimeout(function(){
  Desabilitar(...)
}, 3000);

I hope I have helped.

    
15.11.2018 / 23:20