const camelToSnake = (string, toUpper = false) => {
const preparedString = string.replace(/[A-Z]/g, '_$&');
return toUpper ? preparedString.toUpperCase() : preparedString.toLowerCase();
};
window.addEventListener('DOMContentLoaded', () => {
const params = new URLSearchParams(location.search);
const keys = params.keys();
for (const key of keys) {
const field = document.querySelector(`input[name="${camelToSnake(key, true)}"]`);
if (field !== null) {
field.value = params.get(key);
}
}
});
<meta name="viewport" content="width=device-width, initial-scale=1.0">
const accordions = document.querySelectorAll('.accordion__card');
for (const accordion of accordions) {
const title = accordion.querySelector('.accordion__card__title');
const text = accordion.querySelector('.accordion__card__text');
title.addEventListener('click', () => {
if (text.classList.contains('accordion__card__text--active')) {
text.classList.remove('accordion__card__text--active');
} else {
for (const accordion of accordions) {
const text = accordion.querySelector('.accordion__card__text');
text.classList.remove('accordion__card__text--active');
}
text.classList.add('accordion__card__text--active');
}
});
}
useObserver
- будет пересчитывать увеличение автоматически. (now / total) * 100
:function calculatePercent(value, max) {
return (value / max) * 100;
}
document.querySelectorAll('input[type="number"]').forEach(input => {
input.addEventListener('input', () => {
if (input.value !== '') {
const min = input.min !== '' ? Number(input.min) : -Infinity;
const max = input.max !== '' ? Number(input.max) : Infinity;
const value = Number(input.value);
input.value = Math.max(min, Math.min(value, max));
}
});
});