const sort = {
str: (a, b) => a.text().localeCompare(b.text()),
num: (a, b) => parseInt(a.text(), 10) - parseInt(b.text(), 10),
};
<button class="sort" data-field="age" data-type="num">Сортировать по возрасту</button>
<button class="sort" data-field="name" data-type="str">Сортировать по имени</button>
$('.sort').click(function() {
const
$this = $(this),
data = $this.data(),
compare = sort[data.type],
field = `.${data.field}`,
order = +data.order || 1;
$('.block')
.sort((a, b) => order * compare($(field, a), $(field, b)))
.appendTo('.cont');
$this.data('order', order * -1);
});
$list.find('input').click(e => e.stopPropagation());
if (!$(e.target).is('input')) {
$list.hide();
}
.slice(1)
там, где работаете со строками таблицы. Т.е., вместо$('#data tbody tr').hide();
$('#data tbody tr').slice(0, rowsShown).show();
$('#data tbody tr').slice(1).hide().slice(0, rowsShown).show();
$('#data tbody tr').css('opacity',...
$('#data tbody tr').slice(1).css('opacity',...
str.match(/rgb\(.*\)/).pop().match(/\d+/g)
str.match(/rgb\((.*)\)/).pop().split(', ')
projectTechnologies.innerHTML = technologyHandler(dataTechnologies).map((el) => el);
projectTechnologies.innerHTML = technologyHandler(dataTechnologies).map(n => n.outerHTML).join('');
// или
technologyHandler(dataTechnologies).forEach(n => projectTechnologies.appendChild(n));
technologyHandler
следующим образом:const technologyHandler = str => str
.split(' ')
.map(n => `<p class="project_technologies-item">${n}</p>`)
.join('');
// или
const technologyHandler = str =>
str.replace(/ ?(\S+)/g, '<p class="project_technologies-item">$1</p>');
map
после её вызова:projectTechnologies.innerHTML = technologyHandler(dataTechnologies);
const digits = Object
.entries([...`${num}`].reduce((acc, n) => (acc[n] = (acc[n] || 0) + 1, acc), {}))
.reduce((acc, n) => (n[1] > 1 && acc.push(+n[0]), acc), []);
const digits = num
.toString()
.split('')
.reduce((acc, n) => (acc[n]++, acc), Array(10).fill(0))
.map((n, i) => n > 1 && i)
.filter(n => n !== false);
const digits = Array
.from(String(num), Number)
.filter((n, i, a) => i !== a.indexOf(n))
.filter((n, i, a) => i === a.indexOf(n));
const digits = (('' + num)
.match(/\d/g)
.sort()
.join('')
.match(/(\d)\1+/g) || [])
.map(n => n[0] | 0);
const values = [ 'text', 'move', 'lols' ];
const selector = values.map(n => `[data-cmd="${n}"]`).join(', ');
const elements = document.querySelectorAll(selector);
elements.forEach(n => n.style.display = 'none');
$('.count').on('input', e => $('.game_iframe').width(e.target.value));
document.querySelector('.count').addEventListener('input', function() {
document.querySelector('.game_iframe').style.width = `${this.value}px`;
});
function randomArr(size, min, max) {
const arr = Array.from({ length: max - min + 1 }, (n, i) => i + min);
for (let i = arr.length; --i > 0;) {
const j = Math.random() * (i + 1) | 0;
[ arr[j], arr[i] ] = [ arr[i], arr[j] ];
}
return arr.slice(0, size);
}
const arr = randomArr(4, 1, 9);
const containerSelector = '.dog';
const buttonSelector = 'i.fa-dog';
const counterSelector = '.score_counter';
$(buttonSelector).click(() => {
$(counterSelector).text((i, val) => +val + 1);
});
// или
document.querySelector(buttonSelector).addEventListener('click', () => {
document.querySelector(counterSelector).textContent -= -1;
});
.dog
несколько:$(containerSelector).on('click', buttonSelector, e => {
$(counterSelector, e.delegateTarget).text((i, val) => -~val);
});
// или
document.querySelectorAll(buttonSelector).forEach(function(n) {
n.addEventListener('click', this);
}, ({ currentTarget: t }) => t
.closest(containerSelector)
.querySelector(counterSelector)
.innerText++
);
const $thumbs = $('.thumbs');
$('.show-image').click(function(e) {
e.preventDefault();
$('.images img.active').removeClass('active');
$('.images img').eq(this.dataset.index).addClass('active');
}).attr('data-index', i => i);
$('.next').click(() => $thumbs.append($thumbs.find('.show-image').first()));
$('.prev').click(() => $thumbs.prepend($thumbs.find('.show-image').last()));
<button>toggle disabled</button>
<select>
<option value="">выбрать</option>
<option value="69">hello, world!!</option>
<option value="187">fuck the world</option>
<option value="666">fuck everything</option>
</select>
const $select = $('select');
$('button').click(function() {
$select
.prop('disabled', (i, val) => !val)
.val('')
.find('[value=""]')
.text(() => $select.prop('disabled') ? 'тут ничего нет' : 'выбрать');
});
// или
const select = document.querySelector('select');
document.querySelector('button').addEventListener('click', () => {
select.disabled = !select.disabled;
select.value = '';
select[0].textContent = select.disabled ? 'тут ничего нет' : 'выбрать';
});
const elems = document.querySelectorAll('.hint');
// или
const elems = document.getElementsByClassName('hint');
const getText = el => el.innerText;
// или
const getText = el => el.textContent;
// или
const getText = el => el.firstChild.nodeValue;
const hints = Array.from(elems, getText);
// или
const hints = [].map.call(elems, getText);
// или
const hints = [];
for (const n of elems) {
hints.push(getText(n));
}
// или
const hints = [];
for (let i = 0; i < elems.length; i++) {
hints[i] = getText(elems[i]);
}
const filterSelector = '.s-mobile-filter-type-select';
const selectSelector = '.s-mobile-filter-type-select__select';
const selectedSelector = '.s-mobile-filter-type-select__selected-options-list-content';
$(selectSelector).on('change', function() {
$(this)
.closest(filterSelector)
.find(selectedSelector)
.text($('option:selected', this).get().map(n => $(n).text()).join(', '));
});
const updateSelected = select => select
.closest(filterSelector)
.querySelector(selectedSelector)
.textContent = Array
.from(select.selectedOptions, n => n.text)
.join(', ');
// можно назначить обработчик события каждому select'у индивидуально
document.querySelectorAll(selectSelector).forEach(function(n) {
n.addEventListener('change', this);
}, e => updateSelected(e.target));
// или, применяем делегирование
document.addEventListener('change', ({ target: t }) => {
if (t.matches(selectSelector)) {
updateSelected(t);
}
});