There is a normal input

 <input type="text" name="bin" placeholder="" class="order_input"> 

There are radio buttons, where when you click on one, this Input is visible, when you click on the second, it is hidden.

 <script> $(document).ready(function(){ $( ".individual" ).click(function() { $( ".requi" ).slideUp(500); }); $( ".legal" ).click(function() { $( ".requi" ).slideDown(500); }); }); </script> 

How to make it so that when you clicked, the required attribute was added to the input and was also removed when you clicked on another radio button.

    2 answers 2

    Your task is solved in the following lines:

    Adds an attribute:

     $('.order_input').prop('required', true); 

    Removes attribute:

     $('.order_input').prop('required', false); 

     $(document).ready(function(){ var requi = $('.requi'); var input = $('.order_input'); $( ".individual" ).click(function() { input.prop('required', true); // <<== requi.slideUp(500); }); $( ".legal" ).click(function() { input.prop('required', false); // <<== requi.slideDown(500); }); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="bin" placeholder="" class="order_input"> <button class="individual">individual</button> <button class="legal">legal</button> 

    • Class only correct where removes the attribute to false. - duddeniska

    Adding and deleting an attribute:

    jQuery :

     $('selector').attr('attrName', 'value'); $('selector').removeAttr('attrName'); 

    Analogue is native :

     element.addAttribute('attrName', 'value'); element.removeAttribute('attrName'); 

    Adding and deleting a key on an item (property):

    jQuery :

     $('selector').prop('propertyName', value); 

    Natively :

     element['propertyName'] = value; 

    As a result, it will be released:

     $(document).ready(function(){ $( ".individual" ).click(function() { $( ".requi" ).prop('required', true).slideUp(500); }); $( ".legal" ).click(function() { $( ".requi" ).prop('required', false).slideDown(500); }); }); 
    • disabled and readonly through prop () are enabled. - dlarchikov
    • Yes, hurried. - Pleshevskiy