remove required property from input field on form submit

JavaScript is case sensitive.

Use

document.getElementById("city").required = false;

Demonstration

Be careful that your code can’t work as you try to access the element before it exists. Put your script after the element if you don’t execute the code on an event :

<input type="text" id="city" required>
<script>
if(somecondition is true){
    document.getElementById("city").required = false;
}
</script>

Note also that you can’t change this in a submit function and expect your form to be submitted because it’s too late : this event handler won’t be called if the required field is not filled !

Leave a Comment