const parentSelector = 'div';
const childSelector = 'p';
const className = 'there';
const siblingsSelector = `${parentSelector} > ${childSelector}`;
const elementSelector = `${siblingsSelector}.${className}`;
const index = Array.prototype.findIndex.call(
document.querySelectorAll(siblingsSelector),
n => n.classList.contains(className)
);
let index = -1;
for (
let el = document.querySelector(elementSelector);
el;
index++, el = el.previousElementSibling
) ;
const el = document.querySelector(elementSelector);
const index = el ? [...el.parentNode.children].indexOf(el) : -1;
childSelector
, и они не должны учитываться, то третий вариант не подходит, а во втором надо заменить index++
на index += el.matches(childSelector)
. <select name="list" id="mylist" style="width:140px;">
<option>United States</option>
<option>Austria</option>
<option>Alabama</option>
<option>Jamaica</option>
<option>Taiwan</option>
<option>canada</option>
<option>palau</option>
<option>Wyoming</option>
$('#mylist').select2({
sortResults: function(data) {
/* Sort data using lowercase comparison */
return data.sort(function (a, b) {
a = a.text.toLowerCase();
b = b.text.toLowerCase();
if (a > b) {
return 1;
} else if (a < b) {
return -1;
}
return 0;
});
}
});
function tag(input, key, value) {
if (typeof key === 'string' && value !== undefined) {
return input.replace('{' + key + '}', value);
} else {
for (let index in key) {
input = input.replace('{' + index + '}', key[index]);
}
}
return input;
}
$.easing.bullshit = function(x, t, b, c, d) {
return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
};
$('.ach-number span').each(function() {
$(this).prop('counter', 0).animate({
counter: $(this).text(),
}, {
duration: 10000,
easing: 'bullshit',
step(val) {
$(this).text(Math.ceil(val));
},
});
});
const getCard = num => cards.find(n => n.id === num);
// ...
const card = getCard(cardRandom(1, 51));
id
. Может, лучше использовать случайное число как индекс? Типа так:const card = cards[Math.random() * cards.length | 0];
const nestedVal = {
get(path, root) {
return path.split('.').reduce((p, c) => p[c], root);
},
set(path, root, val) {
path = path.split('.');
const key = path.pop();
nestedVal.get(path.join('.'), root)[key] = val;
}
};