Can you in jQuery do something like (a || b).click()
?
I am trying to have something happen when either a or b is clicked. In the sample below, I want wherever it says button[i]
, for it to have a menubutton[i]
next to it, so that everything can happen either when a button[i]
or menubutton[i]
is clicked.
for (let i = 0; i < 4; i ) {
$(button[i]).click(function() {
if (divClosed[i]) {
$('#icons-pattern-main').css({
'height': '920px',
'clip-path': 'none',
'transition': '0.2s linear'
});
$('#text').css('display', 'none')
$('#textpers').css('display', 'flex')
$('#textpers').text(pers[i])
$('.header').mouseleave(function() {
setTimeout(function() {
$('#textpers').text("τι τάξη είσαι;")
}, 5000);
})
for (let x = 0; x < 4; x ) {
(divClosed[x]) = true
$(ShEl[x]).hide();
}
for (let x = 0; x < 6; x ) {
$(ShElinner[x]).hide();
}
divClosed[i] = false
$(ShEl[i]).css('display', 'flex');
if (i > 1) {
$('#icons-pattern-main').css('height', '520')
for (let x = 0; x < 6; x ) {
$(option[x]).click(function() {
$(ShElinner[x]).show()
$(ShEl[i]).hide()
$('#icons-pattern-main').css('height', '920')
subOpen = true
if (subOpen) {
$(button[i]).click(function() {
$(ShElinner[x]).hide()
subOpen = false
});
}
});
}
}
} else {
$('#icons-pattern-main').css({
'height': '420px',
'clip-path': 'polygon(0 0, 100% 0, 100% 59%, 44% 59%, 50% 100%, 56% 59%, 0 59%)',
'transition': '0.2s linear'
});
divClosed[i] = true;
$(ShEl[i]).hide()
$(ShElinner).hide()
$('#text').css('display', 'flex')
$('#textpers').css('display', 'none')
}
});
}
CodePudding user response:
$('.class1, .class2').on('click', some_function);
Or:
$('.class1').add('.class2').on('click', some_function);
This also works with existing objects:
const $class1 = $('.class1');
const $class2 = $('.class2');
$class1.add($class2).on('click', some_function);
CodePudding user response:
Normally, You can select multiple elements using CSS selectors.
So you'd separate multiple selectors with a comma (,
) like this:
$(".a,.b").click()
In your case, you can add elements to the selector with the .add()
function as Lennard K suggested like this:
$(button[i]).add(menubutton[i]).click()