Although my code works (below), typescript is throwing errors saying "Property 'style' does not exist on type 'HTMLElement'."
As you can see I am grabbing the element via jQuery & the element's ID.
$(this.id).click(function () {
this.style.display = 'none';
});
But this causes the following error:
`TS2339: Property 'style' does not exist on type 'HTMLElement'`.
I am not sure what would be the proper way of handling this ts error.
Would anyone let me know?
Thank you in advance.
more code, since it was requested:
`/**
* Class represents a flash message
*/
export class FlashMessage {
flashMessageParentDivID:string;
iconID:string;
textID:string;
$: JQuery;
/**
*
* @param {string} flashMessageParentDiv - the string of the div's ID.
*/
constructor (flashMessageParentDiv: string, $: JQuery) {
this.flashMessageParentDivID = "#"+flashMessageParentDiv;
this.iconID = "#flash-message-icon",
this.textID = "#flash-message-text";
this.$ = $;
}
/**
* Displays the passed in message with the appropriate status.
* @param {string} message - the message we want to flash.
* @param {string} status: either 'error' or 'success'
*/
showWithMessage = function (message: string, status: string) {
//@todo: maybe throw an error if status does not equal error or success.
this.clearAndCreateFlashMessageInnerds();
this.$(this.flashMessageParentDivID).removeAttr("class");
this.$(this.flashMessageParentDivID).addClass(status);
this.$(this.iconID).removeAttr("class");
this.$(this.iconID).addClass("k-icon k-i-" + status);
this.$(this.textID).text(message);
this.$(this.flashMessageParentDivID).show();
this.$(this.flashMessageParentDivID).click(function () {
(<any>this).style.display = 'none';
});
};
...`
use this in your method
(<HTMLElement>document.querySelector('#yourDomElmentID')).style.display = 'none';
i test your code its compile successfully in my code.
As Manohar Gavit suggested, I needed to typecast. But, his suggestion did not fix my problem. For some reason my jQuery definition file does not recognize HTMLElement
as having the style
property, but as you can see below... the definition file does accept any
with that property.
`$(this.id).click(function () {
(<any>this).style.display = 'none';
});`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With