Edited, See below
I have, for now, two radio buttons, each with a value representing the number of account holders. I.e.:
<INPUT id="ONE_HOLDER" name=NR_OF_HOLDERS value=1 type=radio />
<LABEL for=ONE_HOLDER>One holder</LABEL>
<br />
<INPUT id="TWO_HOLDERS" name=NR_OF_HOLDERS value=2 type=radio />
<LABEL for=TWO_HOLDERS>Two holders</LABEL>
I want to validate everytime one of the radio buttons is selected, so I use the Jquery validator to do the following:
//Validations fix for radio and checkbox checks
$('input[type="radio"],input[type="checkbox"]').change(function(){
$("#YOUR_ACC").validate().element(this);
});
This calls
$('#YOUR_ACC').validate({ ... });
which in turn runs through my custom validator method which is the following:
$.validator.addMethod("numberOfHolders", function(value, element) {
try {
nrOfHolders = value;
alert("The value is " + nrOfHolders);
if(nrOfHolders == 1) { return true; }
else if(nrOfHolders == 2){ return false; }
}
catch(err) {
return false;
}
});
It runs through it and all, but the problem is that when I select the first radio button, which is the one that needs to be selected it keeps that value(1) eventhough I select the second radio button.
Why is that and how do I fix it?
Edit for solution
See answer in repsonse below.
My solution
It seems like value in
Doesn’t take the value of a radio button.
I made it work by getting the correct element using the element parameter (to be sure and more dynamic) and then get the value of the checked radio button like you would do in a regular jQuery method: