0

Don't know why my code not removing and add required to select. I try use .prop('required','required') and attr('required')/attr('required','required) and it's not working when I switching options css attribute work correctly.

$('#pierwszy_wybor_jaka_szkola').change(function () {
        if ($(this).val() == '0') {
            $('.pierwszy-wybor-branzowa').css("display", "none");
            $('.pierwszy-wybor-technikum').css("display", "none");
            $('#pierwszy-wybor-technikum').prop('required', false);
            $('#pierwszy-wybor-branzowa').prop('required', false);

        } else if ($(this).val() == 'Technikum') {
            $('select#pierwszy-wybor-technikum').attr('required','required');
            $('select#pierwszy-wybor-branzowa').removeAttr('required');
            $('.pierwszy-wybor-technikum').css("display", "block");
            $('.pierwszy-wybor-branzowa').css("display", "none");


        } else if ($(this).val() == 'Branżowa') {
            $('#pierwszy-wybor-technikum').prop('required', false);
            $('#pierwszy-wybor-branzowa').prop('required', true);
            $('.pierwszy-wybor-branzowa').css("display", "block");
            $('.pierwszy-wybor-technikum').css("display", "none");

        } else {
            $('#pierwszy-wybor-technikum').prop('required', false);
            $('#pierwszy-wybor-branzowa').prop('required', false);
            $('.pierwszy-wybor-branzowa').css("display", "none");
            $('.pierwszy-wybor-technikum').css("display", "none");
            $('.first-row-empty').css("display", "block");

        }
    });
enc
  • 121
  • 9
  • Yes i try and also not working – enc May 14 '20 at 12:31
  • Does this answer your question? [remove required property from input field on form submit](https://stackoverflow.com/questions/18111915/remove-required-property-from-input-field-on-form-submit) – freedomn-m May 14 '20 at 12:40
  • use attr('required', 'required') – sauhardnc May 14 '20 at 12:41
  • 1
    #1 Be consistent when using `attr()` and `prop()`, they are **not** the same thing. #2 Check your selectors! In 3 of your 4 Conditions you select the element with the id `#pierwszy-wybor-branzowa` and in 1 you select the **select** element with the id `#pierwszy-wybor-branzowa`. Is that element really a select tag? – Lapskaus May 14 '20 at 12:44
  • 1
    Does this answer your question? https://stackoverflow.com/a/7264962/2181514 – freedomn-m May 14 '20 at 12:46
  • 1
    If you use attr, then it's `.attr("required", true)` or `.attr("required", false)` (2nd does work in jquery: https://jsfiddle.net/2de48tw7/1/). It should not be `.attr("required", "required")` - but it shouldn't matter and can be anything other than `false` as required is a value-less attribute. (so the valid HTML would be `` not ``) `.removeAttr` should work fine if you used `.attr` to set it (not prop) - so could be that you've used prop or that your selector is wrong. – freedomn-m May 14 '20 at 12:49
  • Thank you guys, I just change .prop('required',true) to .prop("required", true) and it's working - magic :) – enc May 14 '20 at 12:51

1 Answers1

1

It should be true or false

try this

    $('select#pierwszy-wybor-technikum').attr('required',true);
Sekhar Babu
  • 368
  • 2
  • 8
  • 27