how to work with javascript function for asp.net checkbox control which is placed in contentplaceholder?

StackOverflow https://stackoverflow.com/questions/8296130

Pergunta

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.

Foi útil?

Solução

Use this script instead:

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top