Home > OS >  jquery, OR inside click function
jquery, OR inside click function

Time:03-03

can you in jQuery do something lik (a || b).click()?

I am trying to have somethinbg happen when either a or b is clicked. In the sample below, i want wherever is 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. Thanks in advancne!

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:

You can select multiple elements using CSS selectors.

So you'd separate multiple selectors with a comma (,) like this:

$(".a,.b").click()
  • Related