getElementById() dosen't find <input type="hidden" runat="server"/>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <input id="divPosition" type="hidden" value="12" runat="server" /> <div id="divScroller" style="overflow:scroll; margin-top:10px; height:505px; 100%" onscroll="GetDivPosition(this)"> ... ... </div> ... </asp:Content>
onscroll I am calling Javascript function "GetDivPosition(this)" and trying to save the sroll positon in hidden field. But the javascript function doesn't find the hidden field if I have runat="server" attribute with hidden field. Here is the javascript function
function GetDivPosition(divRef) { //alert(divRef.scrollTop); document.getElementById("divPosition").value = divRef.scrollTop; alert("hidden" + document.getElementById("divPosition").value); }
A server contorl inside Content page will have some prefix added to its ID,when it is generated as HTML.so you'll have to access by using its ClientID like this.
document.getElementById('<%=divPosition.ClientID%>').value=divRef.scrollTop;
注意:使用<%=divPostion.ClientID%>的时候,这段js不能写在单独的文件中,必须写在aspx文件中才可以.