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

109 views Asked by At

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.

2

There are 2 answers

3
Satpal On BEST ANSWER

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);
});
0
Bhojendra Rauniyar On

You didn't set the attribute but get the attribute. So, use like this:

$('.form_field select').attr('required',true);