using jquery I want to make a selection from a select box required (ie it can\'t be left as the blank default) if a particular checkbox is ticked. My code:
$
This works like a charm
inputname:
{
required: function(){
if($("select[name=inputname]").val() == 1){
return true;
}
else
{
return false;
}
}
}
You can edit the inputname to yours prefered.
To change it to input field, you can change the select part to input
Please try like this
$(document).ready(function(){
$("#myForm").validate({
rules: {
myDropDown:{
required: function (element) {
if($("#myCheckbox").is(':checked')){
var e = document.getElementById("myDropDown");
return e.options[e.selectedIndex].value=="" ;
}
else
{
return false;
}
}
}
}
});
});
it should be able to work just like that:
rules : {
myDropDown:{required:"#myCheckbox:checked"}
}
I don't know what plugin you are using, but
return $("#myCheckbox:checked")
returns a Jquery object if your checkbox is checked. I think it would be more correct to do:
return $("#myCheckbox").is(':checked')
which returns true or false.
Maybe this has nothing to to with your problem