Evan Kim
Evan Kim

Reputation: 1

how can i used to loop in jquery?

I'm newbie jquery..

I don't know how can I compacted code for loop.

I have been saw the many code.

$('#fadeandscale1').popup({
    pagecontainer: '.container',
    transition: 'all 0.3s'
});
$('#fadeandscale2').popup({
    pagecontainer: '.container',
    transition: 'all 0.3s'
});
$('#fadeandscale3').popup({
    pagecontainer: '.container',
    transition: 'all 0.3s'
});
$('#fadeandscale4').popup({
    pagecontainer: '.container',
    transition: 'all 0.3s'
});
$('#fadeandscale5').popup({
    pagecontainer: '.container',
    transition: 'all 0.3s'
});
$('#fadeandscale6').popup({
    pagecontainer: '.container',
    transition: 'all 0.3s'
});
$('#fadeandscale7').popup({
    pagecontainer: '.container',
    transition: 'all 0.3s'
});
$('#fadeandscale8').popup({
    pagecontainer: '.container',
    transition: 'all 0.3s'
});

Upvotes: 0

Views: 38

Answers (2)

madalinivascu
madalinivascu

Reputation: 32354

You can use the css3 selector [attribute^=value] that selects a element based on the beginning of a attribute, the id for us

 $('div[id^="fadeandscale"]').popup({ pagecontainer: '.container', transition: 'all 0.3s' })

Upvotes: 2

Tushar
Tushar

Reputation: 87203

You don't need loop for this, you can provide multiple comma-separated selectors as follow:

$('#fadeandscale1, #fadeandscale2, #fadeandscale3, #fadeandscale4, #fadeandscale5, #fadeandscale6, #fadeandscale7, #fadeandscale8').popup({
    pagecontainer: '.container',
    transition: 'all 0.3s'
});

I'll recommend you to use common class to all the elements of interest and use it as selector.

$('.fadeElems').popup({
    pagecontainer: '.container',
    transition: 'all 0.3s'
});

Upvotes: 2

Related Questions