Question

I have checkbox and textbox control from asp.net on the contentplaceholder. I want to call the javascript function for enabling the textbox control depending on the checked status of the checkbox. I have written following javascript for this-

name of the checkbox & textbox respectively after rendering it to browser ctl00$ContentPlaceHolder1$chkCall, ctl00$ContentPlaceHolder1$txtCall

function chkChanged() {
           try {

           var echk = document.getElementsByName('ctl00$ContentPlaceHolder1$chkCall');
           var etxt = document.getElementsByName('ctl00$ContentPlaceHolder1$txtCall');

           if (echk.Checked) {

               etxt.Enabled = true;
           }
           else {
               etxt.Enabled = false;
               etxt.Text = "";
           }
           return true;
           }
       catch (err) {
           alert(err.Message);
           return false;
       }
       }

When i'm executing above script then it calls function but the code is not working for the control. And it is not throwing any exception.

What is wrong in above script?

thanks.

Was it helpful?

Solution

Use this script instead:

var echk = document.getElementById("<%= chkCall.ClientID %>");
var etxt = document.getElementById("<%= txtCall.ClientID %>");

OTHER TIPS

getElementsByName returns an array of elements (plural) try using getElementById to get a single one element, which is probably what you want because you are checking whether echk.Checked == true. In your case echk is an array, not a checkbox.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top