Search code examples
jqueryselectattributesboxparsley

Trying to add a required attribute to a select box when checkbox is ticked - JQuery


I'm having a little trouble adding a 'required' attribute to a select box when a checkbox is checked.

Here's my code so far.

$('.turn_on').change(function(){
    $('.form_field select').attr('required');
});

If anyone could help, that would be greatly appreciated.


Solution

  • Use .prop(propertyName, value) set the property

    $('.turn_on').change(function(){
        //set required to true/false based on checked box checked state
        $('.form_field select').prop('required', this.checked);
    });