HTML DOM Input Color Object

HTMLWeb DevelopmentFront End Technology

The HTML DOM Input Color Object represents an input HTML element with type color.

Syntax

Following is the syntax −

  • Creating an <input> with type color −
var colorObject = document.createElement(“input”);
colorObject.type = “color”;

Attributes

Here, “colorObject” can have the following attributes −

AttributesDescription
autocompleteIt defines the value of autocomplete attribute of a color picker
autofocusIt defines if the color picker should be focused on initial page load.
defaultValueIt sets/returns the default value of color picker
disabledIt defines if color picker is disabled/enabled
formIt returns a reference of enclosing form that contains the color picker
nameIt defines the value of name attribute of a color picker
typeIt returns the type of form element of color picker
valueIt defines the value of the value attribute of a color picker

Example

Let us see an example of Input Color name property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Input Color Name</title>
</head>
<body>
<form id="formForColorsInput">
Color Picker: <input type="color" id="Color" name="primaryColor">
</form>
<button onclick="changeNameValue()">Change name value</button>
<div id="divDisplay"></div>
<script>
   var inputColor = document.getElementById("Color");
   var divDisplay = document.getElementById("divDisplay");
   divDisplay.textContent = 'Name of color input: '+inputColor.name;
   function changeNameValue() {
      if(inputColor.name == 'primaryColor'){
         inputColor.name = 'secondaryColor';
         divDisplay.textContent = 'Name of color input: '+inputColor.name;
      }
   }
</script>
</body>
</html>

Output

This will produce the following output −

Before clicking ‘Change name value’ button −

After clicking ‘Change name value’ button −

raja
Published on 19-Jul-2019 12:32:07
Advertisements