Tengo un código js como este:
lightGallery($("#web1")[0], { selector: 'this', mobileSettings: { controls: false, showCloseIcon: true, download: false, } }); lightGallery($("#web2")[0], { selector: 'this', mobileSettings: { controls: false, showCloseIcon: true, download: true, } }); lightGallery($("#web3")[0], { selector: 'this', mobileSettings: { controls: false, showCloseIcon: true, download: true, } });¿Cómo optimizo el código para no tener que escribir un número nuevo cada vez?
Puedes usar un bucle for :
let lowerLimit = 1; let upperLimit = 3; for (let i = lowerLimit; i <= uppperLimit; i++) { lightGallery($("#web" + i)[0], { selector: 'this', mobileSettings: { controls: false, showCloseIcon: true, download: false, } }); }Considera lo siguiente.
$("[id^='web']").each(function(i, el){ lightGallery($(el)[0], { selector: 'this', mobileSettings: { controls: false, showCloseIcon: true, download: false, } }); }); Esto pasará por encima de cada uno que tenga un ID que inicie web y aplique el mismo código.