.red {
background: red;
}
$('.table').on('change', function(e) {
const $tr = $(e.target).closest('tr');
const ajaxdata = $tr.find('.ajaxdata').val();
const subcat = $tr.find('.subcat').val();
$tr.toggleClass('red', ajaxdata === 'none' && subcat === 'none');
}).find('tr').change();
// или
const table = document.querySelector('table');
table.addEventListener('change', e => {
const tr = e.target.closest('tr');
const ajaxdata = tr.querySelector('.ajaxdata').value;
const subcat = tr.querySelector('.subcat').value;
tr.classList.toggle('red', ajaxdata === 'none' && subcat === 'none');
})
table.querySelectorAll('tr').forEach(n => {
n.dispatchEvent(new Event('change', { bubbles: true }));
});
const mustStay = arr => arr.every(n => n.value !== '-');
const newArr = arr.filter(mustStay);
arr.reduceRight((_, n, i, a) => mustStay(n) || a.splice(i, 1), null);
// или
arr.splice(0, arr.length, ...arr.filter(mustStay));
// или
let numDeleted = 0;
for (const [ i, n ] of arr.entries()) {
arr[i - numDeleted] = n;
numDeleted += !mustStay(n);
}
arr.length -= numDeleted;
const length = Math.max(...arr.map(n => n.length));
for (const n of arr) {
const id = 1 + Math.max(0, ...n.map(m => m.id));
n.push(...Array.from(
{ length: length - n.length },
(_, i) => ({ id: id + i, value: '_' })
));
}
const newArr = arr.map(n => Array.from({ length }, (_, i) => ({
id: i + 1,
value: i < n.length ? n[i].value : '-',
})));
[ 7, 7, 7, 0, 1, 1 ]
? В первом случае 2
- повторяются семёрка и единица, во втором 5
- три семёрки плюс две единицы.const duplicateCount = Object
.values(arr.reduce((acc, n) => (acc[n] = (acc[n] || 0) + 1, acc), {}))
.filter(n => n > 1)
.length;
// или
const duplicateCount = Array
.from(arr.reduce((acc, n) => acc.set(n, acc.has(n)), new Map).values())
.reduce((acc, n) => acc + n, 0);
// или
const duplicateCount = new Set(arr.filter((n, i, a) => i !== a.indexOf(n))).size;
const duplicateCount = Object
.values(arr.reduce((acc, n) => (acc[n] = acc.hasOwnProperty(n), acc), {}))
.reduce((acc, n) => acc - !n, arr.length);
// или
const duplicateCount = Array
.from(arr.reduce((acc, n) => acc.set(n, -~acc.get(n)), new Map).values())
.reduce((acc, n) => acc + (n > 1) * n, 0);
// или
const duplicateCount = arr
.filter((n, i, a) => a.indexOf(n) !== a.lastIndexOf(n))
.length;
const isArrOK = arr => arr.length && arr.every(n => n.value !== '-');
const newArr = arr.reduce((acc, { arr: n }) => (
isArrOK(n) && acc.push(n),
acc
), []);
// или
const newArr = arr.map(n => n.arr).filter(isArrOK);
// или
const newArr = [];
for (const n of arr) {
if (isArrOK(n.arr)) {
newArr.push(n.arr);
}
}
$('#file').on('change', ({ target: { files: [ file ] } }) => {
$('#info-file').html([ 'name', 'size', 'type' ]
.map(n => `<div>${n}: ${file[n]}</div>`)
.join('')
);
});
$("#box1:checked, #box2:checked").val()
$('#logistic input:checked').get().reduce((acc, n) => acc + +n.value, 0)
function GetCookies(name) { <...> return null; } <...> faves.push(fav); <...> var myfaves = JSON.parse(GetCookies('favespages')); faves = myfaves;
faves = myfaves instanceof Array ? myfaves : [];
. const result = tags.filter(n => active.some(m => m.name === n.name));
const result = tags.filter(function(n) {
return this.has(n.name);
}, new Set(active.map(n => n.name)));
const tagsObj = tags.reduce((acc, n) => (acc[n.name] = n, acc), {});
const result = active.reduce((acc, n) => ((n = tagsObj[n.name]) && acc.push(n), acc), []);
$('.photo-link').val(function() {
return $(this).parent().find('.ulightbox').attr('href');
});
document.querySelectorAll('.photo-link').forEach(n => {
n.value = n.parentNode.querySelector('.ulightbox').getAttribute('href');
});
$('#btn').click(function() {
i = (i + 1) % arrLatLng.length;
map.setCenter(arrLatLng[i]);
});
const containerSelector = '.info';
const buttonSelector = `${containerSelector} button`;
const props = [ 'quantity', 'square', 'floor' ];
const { elements } = document.querySelector('form');
document.addEventListener('click', ({ target: t }) => {
if (t.matches(buttonSelector)) {
props.forEach(function(n) {
elements[n].value = this.querySelector(`.${n}`).textContent;
}, t.closest(containerSelector));
}
});
const result = arr.reduce((acc, n, i) => (
acc.push(...(i & 1 ? n : [...n].reverse())),
acc
), []);
for (let i = 0; i < arr.length; ++i) {
const a = i & 1 ? arr[i] : [...arr[i]].reverse();
for (let j = 0; j < a.length; ++j) {
result.push(a[j]);
}
}
id="link"
.this
; а вместо того, чтобы выдумывать какие-то стрёмные id для блоков которые надо показать, поднимайтесь при клике до общего предка блока и кликнутого элемента, и ищите блок через querySelector
:onclick="showTooltip(this)"
function showTooltip(link) {
const div = link.parentNode.querySelector('div');
if (div.style.display === 'none') {
div.style.display = 'block';
link.style.display = 'none';
} else {
div.style.display = 'none';
}
}
document.querySelectorAll('.contacts-number a').forEach(n => {
n.addEventListener('click', onClick);
});
function onClick() {
const div = this.closest('.contacts-number').querySelector('div');
if (div.style.display === 'none') {
div.style.display = 'block';
this.style.display = 'none';
} else {
div.style.display = 'none';
}
}
пока кроме ошибок ничего не получил
document.querySelectorAll('.test').forEach(n => {
n.textContent = n.textContent.replace(/\bAenean\b/g, 'Aenean1');
});
// или
for (const n of document.getElementsByClassName('test')) {
n.innerText = n.innerText.replace(/\bAenean\b/g, '$&1');
}
<div>
<button data-step="-1">-</button>
<button data-step="+1">+</button>
</div>
<textarea cols="30" rows="10"></textarea>
const history = JSON.parse(localStorage.getItem('history')) || [];
let index = history.length - 1;
const $text = $('textarea').change(function() {
index = history.push($text.val()) - 1;
localStorage.setItem('history', JSON.stringify(history));
});
$('[data-step]').click(function() {
goTo(index + +this.dataset.step);
});
goTo(index);
function goTo(newIndex) {
if (0 <= newIndex && newIndex <= history.length - 1) {
index = newIndex;
$text.val(history[index]);
}
}