var peas = ['#p1','#p2','#p3','#p4','#p5'];
$.each(peas, function(i) => {
$(peas[i]).click( function() => {
$(peas[i]).css({'color':'red'});
});
});
An even bigger example. Say you want to make the P you click on red, but want the other ps return to default color. Just make ana function that removes the selected ID from the array of nonPea and voila!
var peas = ['#p1','#p2','#p3','#p4','#p5'],
poppy=(v,i)=>peas.toString().replace(`,${v[i]}`,'').replace(`${v[i]},`,'');
(// get each nonPeaspea =index
['#p5, #p2$.each(peas,i=>///funciton(i){ #p3,code #p4'inside}
(//set up each individual index's functions
$('.peastock').append(`<p ,'#p1,id="p${[i+1]}">I #p5,am #p3${peas[i]}</p>`), #p4'
$(peas[i]).click(()=>(
$(peas[i]).css({"color":"red",'#p1"background-color":"rgba(128, #p20, #p50, #p4'0.1)"}),
$(poppy(peas,i)).css({'color':'black','background-color':'rgba(255,255,255,0.2)'}))))),
$('.peastock').append(`
<div id="ree">ES6 isnt ,'#p1,suitable #p2,for #p3,all #p5'
of jQuery's usage!</div>
<div>Since some functions inside of jQuery's methods dont ,'#p1,require #p2,'this' #p3,or #p4']
;'return'</div>
$.each(peas, function(i){
<div>You $(peas[i]).click(can function(){
learn this by going <a href="https://www.w3schools.com/Js/js_es6.asp">here</a></div>
`),
$(peas[i]"*").css({'color'"margin":'red'"0 auto","padding":"1%"});,
$("* .peastock, .peastock, .peastock $(nonPeas[i]*").css({'color'"background-color":'black'}"rgba(128,0,0,0.1);
"});
});
Source: jQuery .each() The fiddle in action (with updates!)The fiddle in action (with updates!)