Typescript Form reset () not working

I am using typescript to reset the form but it doesn't work, or the typescript compiler (version 1.0.3) doesn't recognize the reset () function. The compiler gives an error

Build: Interface 'HTMLFormElement' incorrectly extends interface 'HTMLElement'. C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.4\lib.d.ts

      

This is the typescript code

var resetForm =document.getElementById(dirtyFormID);
resetForm.reset();

      

When I copied the code above to the js file, it works great.

What is the reason for this?

+3


source to share


2 answers


Since the function getElementById

returns a more general type HTMLElement

, you need to specify the specific version manually:



var dirtyFormID = 'something';
var resetForm = <HTMLFormElement>document.getElementById(dirtyFormID);
resetForm.reset();

      

+11


source


Note what document.getElementById

returns HTMLElement

(as already said here). You have to impose HTMLElement

onHTMLFormElement

The casting should look like this:



var resetForm:HTMLFormElement;
resetForm= <HTMLFormElement>document.getElementById('your form id');
if(resetForm)
    resetForm.reset();

      

0


source







All Articles