const $table = $('table');
const columnIndex = // здесь должен быть номер столбца с числами
const sum = $table
.find('tr:visible')
.get()
.reduce((acc, n) => acc + +$('td', n).eq(columnIndex).text(), 0);
$table
.find('th')
.eq(columnIndex)
.text(`Вес (${sum})`);
const parents = document.querySelectorAll('.news-block-2 tbody tr');const swapChildren = function(el, selectorA, selectorB) {
const a = this(el.children, n => n.matches(selectorA));
const b = this(el.children, n => n.matches(selectorB));
el.append(...Array.from(el.children, n => n === a ? b : n === b ? a : n));
}.bind(Function.prototype.call.bind(Array.prototype.find));parents.forEach(n => swapChildren(n, '.picture-news', '.text-news'));const swapChildren = el => el.append(...[...el.childNodes].reverse());
// или
const swapChildren = el => el.appendChild(el.firstElementChild);
// или
const swapChildren = el => el.insertBefore(el.children[0], null);
// или
const swapChildren = el => el.insertAdjacentElement('beforeend', el.children[0]);
// или
const swapChildren = el => el.prepend(el.querySelector(':scope > :last-child'));
// или
const swapChildren = ({ lastElementChild: el }) => el.previousElementSibling.before(el);
// или
const swapChildren = el => el.cells[1].after(el.cells[0]);
// или
const swapChildren = ({ cells: [ a, b ] }) => a.replaceWith(b, a);parents.forEach(swapChildren);
// или
for (const n of parents) {
swapChildren(n);
}
// или
for (let i = 0; i < parents.length; i++) {
swapChildren(parents[i]);
}
// или
(function next(i, n = parents.item(i)) {
n && (swapChildren(n), next(-~i));
})(0);
X = n => n ? (n & 1 ? '-chirp' : '') + X(n >> 1) + X(n >> 1) : ''
chirp = n => X(n).slice(1)
google.visualization.arrayToDataTable(data, true)
Array
.from(document.querySelectorAll('.obj'))
.filter(n => n.querySelector('.who')?.innerText !== 'Я')
.forEach(n => console.log(n.innerText));
const itemSelector = 'здесь должен селектор элементов, которые надо переместить';
const containerSelector = 'а здесь - селектор контейнеров';
const chunkSize = 3;const $items = $(itemSelector);
$(containerSelector).append(i => $items.slice(i * chunkSize, (i + 1) * chunkSize));
// или
const $containers = $(containerSelector);
$(itemSelector).each((i, n) => $containers.eq(Math.floor(i / chunkSize)).append(n));document.querySelectorAll(containerSelector).forEach(function(n, i) {
n.append(...this.slice(i * chunkSize, -~i * chunkSize));
}, [...document.querySelectorAll(itemSelector)]);
// или
const containers = document.querySelectorAll(containerSelector);
for (const [ i, n ] of document.querySelectorAll(itemSelector).entries()) {
containers[i / chunkSize | 0]?.appendChild(n);
}
$('label > input').change(function() {
$(this).parent().toggleClass('active', this.checked);
});
const containerSelector = '.card_options > div';
const itemSelector = '.option';
const activeClass = 'checked';
const toggleItem = item => item
?.closest(containerSelector)
.querySelectorAll(itemSelector)
.forEach(n => n.classList[n === item ? 'toggle' : 'remove'](activeClass));
// можно добавить обработчик клика каждому элементу индивидуально
document.querySelectorAll(itemSelector).forEach(function(n) {
n.addEventListener('click', this);
}, e => toggleItem(e.currentTarget));
// а можно один раз - делегированный, всей странице
document.addEventListener('click', e => {
toggleItem(e.target.closest(itemSelector));
});
const arr = Object.values(obj);. Дальше есть варианты:const result = arr.sort((a, b) => a.position - b.position).map(n => n.color);const result = arr
.reduce((acc, n) => ((acc[n.position] = acc[n.position] ?? []).push(n.color), acc), [])
.flat();const result = Object.values(Object.fromEntries(arr.map(n => [ n.position, n.color ])));
document.querySelectorAll('.row').forEach(addClass);
// или
for (const n of document.getElementsByClassName('row')) {
addClass(n);
}const addClass = el => el.classList.add(`row-${el.children.length}`);const classes = {
3: 'row-3',
5: 'row-5',
7: 'row-xxx',
};
function addClass(el) {
const className = classes[el.children.length];
if (className) {
el.classList.add(className);
}
}const classes = [
{ max: 3, name: 'row-0-3' },
{ max: 5, name: '' }, // если дочерних элементов 4 или 5, класс не добавляется
{ max: 7, name: 'row-6-7' },
{ max: Infinity, name: 'row-7-x' },
];
function addClass(el) {
const { name } = classes.find(n => n.max >= el.children.length) ?? {};
if (name) {
el.classList.add(name);
}
}const classes = [
[ c => c < 5, 'row-0-4' ],
[ c => c % 2, 'row-odd' ],
[ c => 6 < c && c < 9, 'row-7-8' ],
];
const addClass = ({ classList, children: { length } }) =>
classList.add(...classes.reduce((acc, n) => (
n[0](length) && acc.push(n[1]),
acc
), []));