-1

How get the value of hidden input that is in MVC view, in the JavaScript/Type Script File? Without use of jQuery please.

This is my input:

 <input type="hidden" value="@Model.GridHeader" name="gridheader" id="gridheader" />
 <input type="hidden" value="@Model.GridData" name="griddata" id="griddata" />

I tried these but non are working, they all return null value.

window.onload = function () {
var gridheader = document.getElementById("gridheader").innerHTML;
var griddata = document.getElementById("griddata").innerHTML;
alert(gridheader );

I already tried innerText and textContent.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alma
  • 3,780
  • 11
  • 42
  • 78

2 Answers2

0

An input element hasn't an innerHTML property but a value attribute. So changing your code as below:

window.onload = function () {
    var gridheader = (<HTMLInputElement>document.getElementById("gridheader")).value;
    var griddata = (<HTMLInputElement>document.getElementById("griddata")).value;
    alert(gridheader);
}

it would work.

You could read more about this here.

property 'value' does not exists on type 'HTMLElement' It gave me this, maybe because it is TypeScirpt?

Above I prepended the document.getElemementById(...) with . I wasn't aware of this, since I have no experience with TypeScript. I found the solution here.

Community
  • 1
  • 1
Christos
  • 53,228
  • 8
  • 76
  • 108
  • @fubbe ` – NewToJS Feb 21 '17 at 19:25
  • property 'value' does not exists on type 'HTMLElement' It gave me this, maybe because it is TypeScirpt? – Alma Feb 21 '17 at 19:30
  • 1
    Please see my update. I found the solution here http://stackoverflow.com/questions/12989741/the-property-value-does-not-exist-on-value-of-type-htmlelement So if that will work, please make an upvote also at this link :) and let me know, in order to upvote it :) – Christos Feb 21 '17 at 19:33
0

Got that work with:

 var inputValue = (<HTMLInputElement>document.getElementById('gridheader')).value;
Alma
  • 3,780
  • 11
  • 42
  • 78