$(document).on('input', '.editor', function() {
const content = $('.editor')
.get()
.map(n => `\n <li>${$(n).html()}</li>`)
.join('');
$('textarea').val(`<ul>${content}\n</ul>`);
});
str.replace(/.{4}(?!$)/g, '$&-')chunked(str, 4).join('-')const chunked = (data, chunkSize, slice = data.slice) =>
Array.from(
{ length: Math.ceil(data.length / chunkSize) },
function(_, i) {
return this(i * chunkSize, (i + 1) * chunkSize);
},
(slice instanceof Function ? slice : Array.prototype.slice).bind(data)
);
const items = document.querySelectorAll('.yourself-item__title');
const height = `${Math.max(...Array.from(items, n => n.clientHeight))}px`;
items.forEach(n => n.style.height = height);
.accordion__grandchild__item, а переключаете видимость у родительских элементов - .accordion__grandchild__group. Надо как-то устранить это печальное несоответствие.
$('select').change(function() {
const v = this.value;
$('.sum').text($(`.element${v === '*' ? '' : `.${v}`}`).length);
});document.querySelector('select').addEventListener('change', e => {
const v = e.target.value;
const s = '.element' + (v === '*' ? '' : '.' + v);
document.querySelector('.sum').textContent = document.querySelectorAll(s).length;
});
<select data-prop="type">
<option hidden></option>
<option value="type1">hello, world!!</option>
<option value="type2">fuck the world</option>
<option value="type3">fuck everything</option>
</select>
<br>
<input placeholder="name1" data-prop="name">
<input placeholder="name2" data-prop="name">
<input placeholder="name3" data-prop="name">
<br>
<input placeholder="url1" data-prop="url">
<input placeholder="url2" data-prop="url">
<input placeholder="url3" data-prop="url">
<br>
<input class="value" disabled>
<input class="value" disabled>
<input class="value" disabled>const getValues = prop =>
Array.from(document.querySelectorAll(`[data-prop="${prop}"]`), n => n.value);
document.addEventListener('input', e => {
if (!e.target.dataset.prop) {
return;
}
const type = getValues('type')[0];
const names = getValues('name');
const urls = getValues('url');
document.querySelectorAll('.value').forEach((n, i) => {
n.name = n.value = `xxx[${type}][${names[i]}][${urls[i]}]`;
});
});
if (item.innerText === currentPage) {item.innerText == currentPage+item.innerText === currentPagedocument.body.insertAdjacentHTML('beforeend', `
<div class="container">
<table>
<thead>
<tr>${keys.map(k => `
<th>${k}</th>`).join('')}
</tr>
</thead>
<tbody></tbody>
</table>
<div class="pagination"></div>
</div>
`);
const tableEl = document.querySelector('.container table');
const paginationEl = document.querySelector('.container .pagination');
paginationEl.addEventListener('click', ({ target: t }) =>
t.matches('a') && showPage(+t.textContent)
);
function showPage(page) {
paginationEl.innerHTML = Array
.from(
{ length: Math.ceil(data.length / rows) },
(_, i) => `<a${-~i === page ? ' class="active"' : ''}>${-~i}</a>`)
.join('');
tableEl.tBodies[0].innerHTML = data
.slice(~-page * rows, page * rows)
.map(n => `
<tr>${keys.map(k => `
<td>${n[k]}</td>`).join('')}
</tr>`)
.join('');
}
showPage(1);
const $inputs = $('.input_1');.Array.from($inputs).forEach(n => console.log(n, n.value ? 'я заполнен' : 'я пуст'));
console.log($inputs.get().some(n => n.value) ? 'кто-то заполнен' : 'все пустые');
console.log($inputs.toArray().every(n => n.value) ? 'все заполнены' : 'кто-то пуст');
console.log(Array.prototype.filter.call($inputs, n => n.value), 'мы заполнены');
console.log([...$inputs.not((i, n) => n.value)], 'мы пустые');
function getDateInTimeZone(utcOffset, date = new Date()) {
const utcTime = date.getTime() + date.getTimezoneOffset() * 60000;
return new Date(utcTime + utcOffset * 3600000);
}
const moscowDate = getDateInTimeZone(3);
const newYorkDate = getDateInTimeZone(-5);
const tokyoDate = getDateInTimeZone(9);
const digital_root = num => num > 9
? digital_root([...`${num}`].reduce((acc, n) => acc + +n, 0))
: num;
const getNested = (obj, ...keys) => keys
.flat(Infinity)
.flatMap(n => typeof n === 'string' ? n.split('.') : n)
.reduce((p, c) => p?.[c] ?? null, obj);getNested(data, [ 'hosts', 1, null ]) // 3
getNested(data, 'hosts', 0) // {name: 'web1'}
getNested(data, 'hosts.0.name.2') // 'b'
getNested(data, [ 'user', 'xxx' ]) // null
getNested([], 'constructor.prototype', [[['slice']]], ['name.length']) // 5
Object.keys(obj).forEach(n => n !== key && delete obj[n]);obj = { [key]: obj[key] };const deleteKeys = (obj, except) =>
Object.keys(obj).forEach(n => except.includes(n) || delete obj[n]);
const obj = { a: 1, b: 2, c: 3, d: 4 };
deleteKeys(obj, [ 'a' ]);
console.log(obj); // {a: 1}const pick = (obj, keys) =>
Object.fromEntries(keys.map(n => [ n, obj[n] ]));
const obj = { a: 1, b: 2, c: 3, d: 4 };
console.log(pick(obj, [ 'a', 'd' ])); // {a: 1, d: 4}
const className = 'select';
const sumEl = document.querySelector('.sum');
const sum = elements =>
Array.prototype.reduce.call(
elements,
(acc, n) => acc + (+n.value || 0),
0
);const updateSum = () => sumEl.textContent = sum(document.getElementsByClassName(className));
document.addEventListener('change', e => e.target.classList.contains(className) && updateSum());
updateSum();const selects = document.querySelectorAll(`.${className}`);
const updateSum = () => sumEl.innerText = sum(selects);
selects.forEach(n => n.addEventListener('change', updateSum));
updateSum();
$('.id100').on('mouseenter mouseleave', e => {
$('ul.fade')
.stop()
.slideToggle(200)
.children()
.toggleClass('active', e.type === 'mouseenter');
});
.submenu, он должен находится внутри того же li, что и соответствующий ему .profile-menu-trigger.$('.profile-menu')
.on('mouseenter', '.profile-menu-trigger', function() {
$(this).next('.submenu').fadeIn(500);
})
.on('mouseleave', 'li', function() {
$(this).find('.submenu').fadeOut(500);
});
console.log(Object.entries(obj).reduce((max, n) => n[1] > max[1] ? n : max).join(': '))