remove required property from input field on form submit

You shoud do this:

if(somecondition is true)
{
  var city = document.getElementById("city");
  city.removeAttribute('required');
}

By the time the On Submit function is to remove the required attribute, the form would have already passed validation. The steps are first you have to disable validation then you will be able to submit the form with the required fields empty. Next remove the required attributes and finally manually invoke validation via $.validator.unubtrusive.parse(form) and this will now validate the form by invoking the other validation types such as string length and formatting, everything else except the required attributes. Check if form.valid() then submit() else do nothing.


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 !


You can use:

document.getElementById("city").removeAttribute("required");

or with jQuery

$('#city').removeAttr('required')