0

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.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
finners
  • 875
  • 16
  • 31

2 Answers2

1

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);
});
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • Thanks for replying! Have tried that but it's still not working. Have added a fiddle here - https://jsfiddle.net/rc59xus2/ – finners Jun 23 '15 at 11:27
  • @FrazerFindlater https://jsfiddle.net/satpalsingh/whkysuf8/ you added code in wrong box – Satpal Jun 23 '15 at 11:29
  • 1
    That's embarrassing! Thanks for that. I see it's working in the fiddle, however, it's not working in my environment. I think it might have something to do with me using parsley.js as well. – finners Jun 23 '15 at 11:36
0

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

$('.form_field select').attr('required',true);
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231