I have some checkboxes as shown
</tr>
<td style="padding-left: 10px" width="70px">
<asp:CheckBox ID="ChkDytLek" runat="server" class="llogariDyt" GroupName="Monedha" Text="Lek" CssClass="radioMarginLeft" OnClick="ChkValidate()" /> <img src="images/eagle-clipart-albanian-7.jpg" width="24px" height="15px" />
</td>
<td>
<asp:CheckBox ID="ChkDytCAD" runat="server" GroupName="Monedha" Text="CAD" CssClass="radioMarginLeft" class="llogariDyt" OnClick="ChkValidate()" /> <img src="images/CAD.png" width="24px" height="15px" />
</td>
</tr>
<tr>
<td> </td>
<td style="padding-left: 10px">
<asp:CheckBox ID="ChkDytEU" runat="server" GroupName="Monedha" Text="EUR" CssClass="radioMarginLeft" class="llogariDyt" OnClick="ChkValidate()" /> <img src="images/eu.png" width="24px" height="15px" />
</td>
</tr>
But I want to limit the user to select not more than 2 of them. I am using a java Script function, but I cant tell why the function is called but enters only in alert(1) and doesn't get the values of my check boxes.
I am calling them the wrong way or something?
function ChkValidate() {
var NewCount = 0
if (document.getElementsByClassName("chkDytLek").checked = true) {
NewCount = NewCount + 1
}
alert(2);
if (document.getElementById("chkDytUSD").checked = true) {
NewCount = NewCount + 1
}
alert(3);
if (document.getElementById("chkDytEUR").checked = true) {
NewCount = NewCount + 1
}
if (document.getElementById("chkDytCAD").checked = true) {
NewCount = NewCount + 1
}
if (document.getElementById("chkDytCHF").checked = true) {
NewCount = NewCount + 1
}
if (NewCount == 3) {
alert('Pick Just Two Please')
document.forms;
return false;
}
}
if(a = true)does not do what you think it does. It's not checking if a is true, it's assigning the value true to a (and returning the result of the operation, which is also true, so theif()condition will always be satisfied). You want to useif(a === true), or simplyif(a).