lundi 16 octobre 2017

chklist is not defined error when trying to capture a checkbox value

I have 2 checkboxes in my web form, on click - i try to capture the checkbox if its clicked on, and display a value in a textbox, here is my html code and Javascript.

HTML:

<tr>
    <td>
       4. Have you ever.....?
    </td>
    <td style="text-align:center;">
      <asp:CheckBox ID="chk1Drink" runat="server" ClientIDMode="Static" onclick="GetAndSetSCORE()" />
     </td>
     <td style="text-align:center;">
       <asp:CheckBox ID="chk1Drug" runat="server" ClientIDMode="Static" onclick="GetAndSetSCORE()" />
     </td>
  </tr>

 <tr>
    <td colspan="3" style="padding-top:10px; padding-right:30px; text-
      align:right;"><b>SCORE:</b>&nbsp;
      <asp:TextBox ID="txtScore" runat="server" ClientIDMode="Static" onkeypress="return false" TabIndex="-1" Width="50px"></asp:TextBox>
    </td>
 </tr>

Javascript:

function GetAndSetSCORE() {
    try {

        var ChkList = ["chk1Drink", "chk1Drug"];

        var Score = 0;
        var txtScore = document.getElementById("txtScore");

//Testing value here....
alert(txtScore);


        for (var i = 0; i < ChkList.length; i++) {
           alert(Chklist[i]);
            var chkbox = document.getElementById(ChkList[i]);
            if (chkbox.checked == true) {
                Score = parseInt(Score) + 1;
            }
        }
        //alert("here2");
        if (txtScore != null) {
            txtScore.value = Score;
        }
    }
    catch (e) {
        alert("Script Error : " + e.message);
    }
}

So the form itself opens up just fine, as soon as I click either the 1st or 2nd checkbox I getthis....

 [object HTMLInputElement]

this correlates to

 alert(txtScore);

After that I get an error that says...

Script Error : Chklist is not defined




Aucun commentaire:

Enregistrer un commentaire