Created
February 2, 2023 19:05
-
-
Save modos/1bccee9be04f82353689ccc760eab190 to your computer and use it in GitHub Desktop.
نمایش رنگها
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>HTML Color Test</title> | |
<style type="text/css"> | |
#color_preview { | |
width: 200px; | |
height: 200px; | |
border: 1px solid grey; | |
background-color: white; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>HTML Color Test</h1> | |
<input id="color_input" type="text" /> | |
<br /><br /> | |
<div id="color_preview"></div> | |
</body> | |
<script> | |
let reg = /^([0-9a-f]{3}){1,2}$/i; | |
let div = document.getElementById('color_preview'); | |
let input = document.getElementById('color_input'); | |
div.style.backgroundColor = 'black'; | |
input.addEventListener('input', (e) => { | |
console.log(reg.test(input.value)) | |
if (reg.test(input.value)) { | |
div.style.backgroundColor = '#' + input.value; | |
} else { | |
div.style.backgroundColor = 'black'; | |
} | |
}) | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment