Document.getElementById ('id'). Value does not work in ASP.net javascript function

Hidden fields:

<input type="hidden" id="hidOrg1" runat="server" value="" /> <input type="hidden" id="hidBT" runat="server" value="" /> 

Javascript function:

 function doGetWave(obj) { //debugger var brk = document.getElementById('hidBT').value; //var brkId = document.getElementById('hidBI').value; var org = document.getElementById('hidOrg1').value; session = obj.options[obj.selectedIndex].value; sWaveText = obj.options[obj.selectedIndex].text; if (brk == "") { window.location.href = "url.aspx?multiple=" + org + "&wave=" + sWaveText + "&strORGId=multiple"; } else { window.location.href = "url.aspx?multiple=" + org + "&wave=" + sWaveText + "&BRKType=" + brk + "&strORGId=multiple"; } } 

separated code:

 protected void Page_Load(object sender, EventArgs e) { hidOrg1.Value = strOrgId; hidBT.Value = ""; } 

A javascript function error with "Object Expected" in var brk = ... and I cannot figure out where this is happening. I stretch my hair !: (

+7
source share
1 answer

When you place runat="server" in a standard HTML tag, ASP.Net manages the identifier to ensure its uniqueness (like its own controls). You need to access the element using the identifier assigned by ASP.Net. Try the following:

 var brk = document.getElementById('<%= hidBT.ClientID %>').value; var org = document.getElementById('<%= hidOrg1.ClientID %>').value; 

Additional Information

If you are using framework 4.0, you can change this behavior at the element, page, or application level. Check out this link for a decent little tutorial. If you decide to install ClientIdMode on Static , you can access your items by the identifier that you originally assigned (they will not be changed).

+16
source

All Articles