UncleCoder.com

UncleCoder.com

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: 515

Edited on 09 Aug 2017



DEMO

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">
<head>
    <title>Demo for add/ Remove Required attribute using javascript</title>
</head>
<body>

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

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

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

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

        </script>
</body>
</html>

 



Leave a Comment:


Advertisement