Trigger click in Typescript - Property 'click' does not exist on type 'Element'
Use the type HTMLElement instead of Element. HTMLElement inherits from Element. And in the documentation you can find that click
function is defined in the HTMLElement.
Cast your element into the HTMLElement via
let element: HTMLElement = document.getElementsByClassName('btn')[0] as HTMLElement;
element.click();
Property 'onclick' does not exist on type 'Element'
As the user above mentioned, you have to cast it to HTMLElement, but inside the for loop. I don't have reputation to comment, thats why i answered again.
ngOnInit() {
const acc = document.getElementsByClassName('accordion');
let i;
for (i = 0; i < acc.length; i++) {
HTMLElement(acc[i]).onclick = function() {
this.classList.toggle('active');
this.nextElementSibling.classList.toggle('show');
};
}
}
Or I would rather recommend using the "addEventListener" function, if you use "document.getElementsByClassName" anyways.
ngOnInit() {
const acc = document.getElementsByClassName('accordion');
let i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click",function() {
this.classList.toggle('active');
this.nextElementSibling.classList.toggle('show');
});
}
}
Property 'click' does not exist on type 'never'. TS2339
TypeScript can't infer the type of the ref from where you use it later in the code, you have to tell it the type of the ref:
const ref = useRef<HTMLDivElement>(null);
// −−−−−−−−−−−−−−−^^^^^^^^^^^^^^^^
(useRef
adds the null
type to the type parameter you give it, soyou don't have to use <HTMLDivElement | null>
.)
Property 'click' does not exist on type 'DebugElement'
You can use JavaScript selector like this
const button = fixture.debugElement.nativeElement.querySelector('.btnClass');
button.click();
TS2339: Property 'style' does not exist on type 'Element'
You need a typecast:
Array.from(document.getElementsByClassName('mat-form-field-infix') as HTMLCollectionOf<HTMLElement>)
That's because getElementsByClassName
only returns HTMLCollection<Element>
, and Element
does not have a style
property. The HTMLElement
however does implement it via it's ElementCSSInlineStyle
extended interface.
Note that this typecast is typesafe in the way that every Element
is either a HTMLElement
or an SVGElement
, and I hope that your SVG Elements don't have a class.
querySelector with trigger click not working into angular 2
Your question is missing a lot of details but can you try to explicitly cast it as an HTMLElement:
let yourElem= <HTMLElement>document.querySelector('.class');
yourElem.click();
Related Topics
Enabling/Disabling Inputs Based on Checkbox Activity
Why Is React Webpack Production Build Showing Blank Page
How to Display JavaScript Datetime in 12 Hour Am/Pm Format
How to Use Fetch Within a For-Loop, Wait for Results and Then Console.Log It
How to Do Validation for 10 Digit Mobile Number Using Jquery
How to Escape an Ampersand in a JavaScript String So That the Page Will Validate Strict
Convert an Object With Multiple Values Per Key into Array of Objects in Js
Invalid Configuration Object in Webpack
Easiest Way to Mask Characters in Html(5) Text Input
Does JavaScript Guarantee Object Property Order
How to Check If Character Is a Letter in JavaScript
How to Check If Checkbox Is Checked With Protractor, Cucumberjs and Chai
Ant Design Range Picker Disable Array of Dates
How to Add Disable Option or Default Option in Select Box
Programmatically Disabling Chrome Auto-Fill
Why Am I Getting an Expected an Assignment or Function Call and Instead Saw an Expression
How to Change Background Color of a Row on Checkbox Selection
How to Format a Number to 2 Decimal Places, But Only If There Are Already Decimals