Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (35 loc) · 1.41 KB
/
Copy pathindex.js
File metadata and controls
42 lines (35 loc) · 1.41 KB
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
40
41
42
const inputField = document.getElementById('inputField');
const outputField = document.getElementById('outputField');
if (inputField && outputField) {
inputField.addEventListener('keyup', () => {
outputField.textContent = inputField.value;
});
}
// Helper to apply text transforms
const applyTransform = (transformFn) => {
if (outputField) {
outputField.textContent = transformFn(outputField.textContent);
}
};
document.querySelector('.uppercase')?.addEventListener('click', () => {
applyTransform(text => text.toUpperCase());
});
document.querySelector('.lowercase')?.addEventListener('click', () => {
applyTransform(text => text.toLowerCase());
});
document.querySelector('.capitalize')?.addEventListener('click', () => {
applyTransform(text => text.charAt(0).toUpperCase() + text.slice(1).toLowerCase());
});
// Helper to toggle CSS style classes
const toggleStyle = (btnSelector, styleProp, activeValue, inactiveValue) => {
const btn = document.querySelector(btnSelector);
btn?.addEventListener('click', () => {
if (outputField) {
const isActive = btn.classList.toggle('active');
outputField.style[styleProp] = isActive ? activeValue : inactiveValue;
}
});
};
toggleStyle('.bold', 'fontWeight', '700', '400');
toggleStyle('.italic', 'fontStyle', 'italic', 'normal');
toggleStyle('.underline', 'textDecoration', 'underline', 'none');