UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Add reuired attribute HTML 5 control - Javascript

Demo and code for how to add required attribute in HTML 5 control

by Athil


Posted on 27 Jun 2018 Category: Javascript Views: 22

Edited on 28 Jun 2019


Here I am going to show how to add required attribute for text control in HTML using javascript.

DEMO

 document.getElementById('txtid').required = true;

where 'txtid' is the controlid

Full Demo code

HTML

<!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>

</body>
</html>

Javascript

 function AddRequired()
        {
            document.getElementById('txtid').required = true;
        }
        function RemoveRequired()
        {
            document.getElementById('txtid').required = false;
        }

 



Leave a Comment: