$('.select-list').change(function() {
const selected = $(':checked', this)
.parent()
.get()
.map(n => $(n).text().trim())
.join(', ');
$('.selected-name--filter').text(selected || 'Любой');
}).change();const select = document.querySelector('.select-list');
select.addEventListener('change', function() {
const selected = Array
.from(this.querySelectorAll(':checked'), n => n.parentNode.textContent.trim())
.join(', ');
document.querySelector('.selected-name--filter').textContent = selected || 'Любой';
});
select.dispatchEvent(new Event('change'));
$('a').attr('title', function() {
return $(this).text();
});document.querySelectorAll('a').forEach(n => n.title = n.innerText);for (const n of document.getElementsByTagName('a')) {
n.setAttribute('title', n.textContent);
}for (let i = 0; i < document.links.length; i++) {
const n = document.links[i];
if (n.tagName === 'A') {
const title = document.createAttribute('title');
title.value = n.innerHTML;
n.attributes.setNamedItem(title);
}
}
$('select').change(function() {
$('button').prop('disabled', $(this).val() === '0');
});document.querySelector('select').addEventListener('change', function() {
document.querySelector('button').disabled = this.value === '0';
});
const createTreeNode = title => ({ id: null, title, isDirectory: false });
const createTree = data =>
data.reduce((root, { id, name }) => {
(id === 0 ? [] : name.slice(1, -1).split('/')).reduce((parent, title) => {
const c = parent.children = parent.children || [];
parent.isDirectory = true;
return c.find(n => n.title === title) || (c[c.length] = createTreeNode(title));
}, root).id = id;
return root;
}, createTreeNode(data.find(n => n.id === 0).name.slice(1, -1)));
const input = document.querySelector('input');
const button = document.querySelector('button');
const num = 6;input.addEventListener('input', e => {
button.disabled = e.target.value % num;
});button.addEventListener('click', () => {
if (input.value % num) {
alert('такого нам не надо, пробуй ещё');
}
});input.addEventListener('change', ({ target: t }) => {
t.value = Math.max(0, (t.value / num | 0) * num);
});<button data-step="-1">-</button>
<button data-step="+1">+</button>document.querySelectorAll('[data-step]').forEach(function(n) {
n.addEventListener('click', this);
}, e => input.value = Math.max(0, +input.value + num * e.target.dataset.step));
const data = $('.price')
.filter((i, n) => +n.value)
.closest('tr')
.find('input[type="hidden"]')
.get()
.map(n => n.value);const data = Array
.from(document.querySelectorAll('.price'))
.filter(n => +n.value)
.map(n => n.closest('tr').querySelector('input[type="hidden"]').value);const data = Array.prototype.reduce.call(
document.getElementsByClassName('price'),
(acc, { value: v, parentNode: { parentNode: tr } }) => (
+v && acc.push(tr.cells[0].children[0].value),
acc
),
[]
);
showFirstHideLast('table', 'tr', 3);.function showFirstHideLast(containerSelector, itemSelector, hideFrom) {
$(containerSelector).each((_, n) => {
$(itemSelector, n).show().slice(hideFrom).hide();
});
}function showFirstHideLast(containerSelector, itemSelector, hideFrom) {
document.querySelectorAll(containerSelector).forEach(n => {
n.querySelectorAll(itemSelector).forEach((m, i) => {
m.hidden = i >= hideFrom;
// или
m.style.display = i < hideFrom ? '' : 'none';
// или (в стили надо будет добавить .hidden { display: none; })
m.classList.toggle('hidden', i >= hideFrom);
});
});
}
когда оба поля заполнены нулями, необходимо выводить всю таблицу
(!min && !max).
function setNested(root, ...args) {
const val = args.pop();
const key = (args = args
.flat(Infinity)
.flatMap(n => typeof n === 'string' ? n.split('.') : n))
.pop();
args.reduce((p, c) => p[c] = p[c] || {}, root)[key] = val;
}const obj = {};
setNested(obj, 'xxx', 'yyy', 'zzz', 69);
setNested(obj, 'xxx.a.b.c', 187);
setNested(obj, [ [ [ '_' ], '?' ], '!' ], 666);
Не могу понять как правильно написать цикл перебора.
$('.hello').attr('data-class', function() {
return [...this.classList].filter(n => n !== 'hello');
});document.querySelectorAll('.hello').forEach(n => {
n.dataset.class = n.className.replace(/(^| )hello( | $)/, ' ').trim();
});$('.hello').attr('data-class', function() {
return this.classList[1];
});document.querySelectorAll('.hello').forEach(n => {
n.dataset.class = n.className.split(' ').pop();
});
Array.prototype.push.apply(
arr,
newArr.filter(n => !arr.some(m => m.trade_id === n.trade_id))
);const pushDiff = (target, source, key = n => n) =>
target.push(...source.filter(function(n) {
return !this.has(key(n));
}, new Set(target.map(key))));
pushDiff(arr, newArr, n => n.trade_id);
const inputSelector = '#kmOutMKAD';
const radioSelector = '[name="delivery"]';
const disabledValue = '1';$(radioSelector).on('change', function() {
$(inputSelector).prop('disabled', this.value === disabledValue);
});
// или
const input = document.querySelector(inputSelector);
const radios = document.querySelectorAll(radioSelector);
const onChange = e => input.disabled = e.target.value === disabledValue;
radios.forEach(n => n.addEventListener('change', onChange));
const { classList: cl } = document.querySelector('#overlay');
const activeMenuClass = 'show-menu';
const matches = (el, ...selectors) => selectors.some(n => el.matches(n));
document.addEventListener('click', ({ target: t }) => {
if (matches(t, '#close-menu', 'li > a')) {
cl.remove(activeMenuClass);
} else if (matches(t, '#open-menu')) {
cl.add(activeMenuClass);
}
});