Solution 1 :

You can add a change event listener to the select.

const input = document.querySelector('input');
const select = document.getElementById("shipping_option");
input.required = select.value === '1';
select.addEventListener('change', e=>{
  input.required = select.value === '1';
});

Problem :

<select required name="shipping_option" id="shipping_option">
  <option selected value="1">1</option>
  <option value="2">2</option>
</select>
    

I have this select and this input

<input  type="text" name="input" placeholder="input">

How I can make this input required when the selected option is 1?

Comments

Comment posted by How to set HTML5 required attribute in Javascript?

Does this answer your question?

Comment posted by j08691

What JavaScript have you tried?

By