Skip to content Skip to sidebar Skip to footer

Displaying "required" In Just One Checkbox (dynamic Display)

I've created a database-driven test with three types of questions - input type = radio, text and checkbox. If I insert required before the closing tag, it works fine for the radio

Solution 1:

Use required using a condition for the first checkbox of a group. I assume your html code is written in .php files.

$required = ($Value == 0) ? "required" : ""; //condition to check if the value is first. since it is database driven it would be better to check the key instead of the value.;echo'<label for="q'.$QID.'-'.$Value.'"><input type="checkbox" name="q'.$QID.'[]" id="q'.$QID.'-'.$Value.'" value="'.$Value.'" '. $required .'> '.$QA.'</label>';

Another solution is to add required attribute using Jquery. For eg if gender is the name of the group of checkboxes, then:

$('input[type=checkbox][name=gender]')[0].attr('required', 'required');;

Post a Comment for "Displaying "required" In Just One Checkbox (dynamic Display)"