Open In App

HTML | DOM Input Color Object

The Input Color Object property in HTML DOM is used to create and access the <input> element within the object. The <input> is used to enter data in the input field. Declaration of input control that allow user to input data is can be done by <input> elements are used within a <form>. 

Syntax:



var x = document.getElementById("myColor");
var x = document.createElement("INPUT");

Property Values:

Example 1: This example describes the getElementById() method to access <input> element with type = “color” attribute. 






<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Input Color Object
    </title>
</head>
<body>
    <h2>
        HTML DOM Input Color Object Property
    </h2>
    <p>
        Select your favorite color:
        <input type = "color" value = "#009900"
            id = "color">
    </p>
    <p>Click on the button to get the color value</p>
    <button onclick = "myGeeks()">
        Click Here!
    </button>
    <p id = "GFG"></p>
     
    <!-- script to return the input color -->
    <script>
        function myGeeks() {
            var x = document.getElementById("color").value;
            document.getElementById("GFG").innerHTML = x;
        }
    </script>
</body>
</html>

Output: 

Before Click on the button:

  

After Click on the button:

  

Example 2: This example describes the document.createElement() method to create <input> element with type = “color” attribute. 




<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Input Color Object
    </title>
</head>
<body>
    <h2>
        HTML DOM Input Color Object Property
    </h2>
    <button onclick = "myGeeks()">
        Click Here!
    </button>
     
    <!-- script to create input color element -->
    <script>
        function myGeeks() {
             
            /* Create input element */
            var x = document.createElement("INPUT");
             
            /* Set color attribute */
            x.setAttribute("type", "color");
             
            /* Set color value */
            x.setAttribute("value", "#009900");
             
            document.body.appendChild(x);
        }
    </script>
</body>
</html>

Output: 

Before Click on the button:

  

After Click on the button:

  

Supported Browsers: The browser supported by DOM Input Color Object property are listed below:


Article Tags :