Free programming examples and instructions

Remove required attribute of HTML 5 Control

How to remove and add required attribute from html 5 control

by Athil

Posted on 10 Jun 2017 Category: Javascript Views: 1954

Edited on 25 Jul 2018


Remove Required attribute

Hi, Here I am going to show how to remove required attribute from HTML 5 Controls using javascript.

document.getElementById('control-id').required = false;

Add required attribute

document.getElementById('control-id').required = true;

Full Demo Code

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <title>Demo for add/ Remove Required attribute using javascript</title>

    <h1>Demo for add/ Remove Required attribute using javascript </h1>

    <input id="txtid"  type="text"/>

    <button type="submit" > Submit </button> <br/><br/>

    <button onclick ="AddRequired();"> Add required </button> 
    <button onclick ="RemoveRequired();"> Remove Required </button>
        function AddRequired()
            document.getElementById('txtid').required = true;
        function RemoveRequired()
            document.getElementById('txtid').required = false;



Leave a Comment: