Home Ask Login Register

Developers Planet

Your answer is one click away!

aarti February 2016

how to access and set the value of server control in javascript

I am in need to access the hidden field and set its value to something, So that I can access the hidden field value in my C# code behind.

i used the following:

var a = document.getElementById(id).getAttribute("value", true); 

but it is giving the undefined or object is null error.

Any Help would be much appreciated.

<asp:Button ID="backbtn" runat="server"  Text="Go Back" OnClientClick="CallConfirmBox('<%= TextBox2.ClientId %>)';" onclick="btnback_click" />
<br/> 
<script type="text/javascript">
            function CallConfirmBox(id) {
                var usrresponse;
                alert("in call confirm box called");
                if ('<%=this.test%>') {

                    var usrresponse = window.confirm("Do you want to go back without saving the data");
                }
                if (usrresponse) //userresponse is true
                {
                    alert(usrresponse);
                   window.location = window.location.href;
                }
                else
               {
                  var e  =document.getElementById(id).getAttribute("value",true);
                    alert(e)
                }
            }
     </script>

Answers


JaydipJ February 2016

To access server control in JS

var control= document.getElementById('<%=URControlID.ClientID%>')

UPDATED As per your code,it seems that you are trying to access value field of button which is not exist if I am not wrong you are trying to access button text

 var e  =document.getElementById(id).getAttribute("Text",true);


RIYAJ KHAN February 2016

Put everything iside

window.onload = function(){

     function CallConfirmBox(id) {
                  //your code
              }
}

Your accessing the element before DOM finish its loading.that's why you getting such error.

EDIT :

suppose id of button is backbtn as given in your html.

window.onload = function(){

         var CallConfirmBox = function (id) {
                      //your code
                  }

        var backBtn = document.getElementById('backbtn');

        backBtn.onclick = CallConfirmBox;

    }

Post Status

Asked in February 2016
Viewed 2,016 times
Voted 10
Answered 2 times

Search




Leave an answer


Quote of the day: live life