UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Add required attribute to html 5 control

How to add required attribute of HTML 5 controls using javascript

by Athil


Posted on 10 Jun 2017 Category: Javascript Views: 267

Edited on 09 Aug 2017



DEMO

Add required attribute

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

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

 

Remove Required attribute

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

 

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