forked from Sulagna-Dutta-Roy/GGExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
39 lines (34 loc) · 1.47 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('getAsciiValueButton').addEventListener('click', displayAsciiValues);
document.getElementById('clearButton').addEventListener('click', clearInput);
document.forms["myform"]["charInput"].addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
event.preventDefault();
displayAsciiValues();
}
});
});
function displayAsciiValues() {
const charInput = document.forms["myform"]["charInput"].value;
const asciiValueDiv = document.getElementById("asciiValue");
asciiValueDiv.classList.remove('opacity-0'); // Reset animation
void asciiValueDiv.offsetWidth; // Trigger reflow
if (charInput.length > 0) {
let asciiValues = [];
for (let i = 0; i < charInput.length; i++) {
asciiValues.push(`${charInput[i]}: ${charInput.charCodeAt(i)}`);
}
asciiValueDiv.innerHTML = asciiValues.join('<br>');
asciiValueDiv.style.color = '#333';
asciiValueDiv.classList.add('opacity-100');
} else {
asciiValueDiv.textContent = "Please enter a word or sentence.";
asciiValueDiv.style.color = 'red';
asciiValueDiv.classList.add('opacity-100');
}
}
function clearInput() {
document.forms["myform"]["charInput"].value = '';
document.getElementById("asciiValue").textContent = '';
document.getElementById("asciiValue").classList.remove('opacity-100');
}