Skip to main content
Optimization on the last section without hasseling an Anti-Array
Source Link
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!)

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 an array of nonPea and voila!

var peas = ['#p1','#p2','#p3','#p4','#p5']
,   nonPeas = ['#p5, #p2, #p3, #p4'
              ,'#p1, #p5, #p3, #p4'
              ,'#p1, #p2, #p5, #p4'
              ,'#p1, #p2, #p3, #p5'
              ,'#p1, #p2, #p3, #p4']
;
$.each(peas, function(i){
    $(peas[i]).click( function(){
        $(peas[i]).css({'color':'red'});
        $(nonPeas[i]).css({'color':'black'});
    });
});

Source: jQuery .each() The fiddle in action (with updates!)

var peas = ['#p1','#p2','#p3','#p4','#p5'];
$.each(peas, i => {
    $(peas[i]).click(() => {
        $(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 a function that removes the selected ID from the array and voila!

var peas = ['#p1','#p2','#p3','#p4','#p5'],
    poppy=(v,i)=>peas.toString().replace(`,${v[i]}`,'').replace(`${v[i]},`,'');

(// get each pea index
  $.each(peas,i=>///funciton(i){ code inside}

    (//set up each individual index's functions
      $('.peastock').append(`<p id="p${[i+1]}">I am ${peas[i]}</p>`),
      $(peas[i]).click(()=>(
        $(peas[i]).css({"color":"red","background-color":"rgba(128,0,0,0.1)"}),
        $(poppy(peas,i)).css({'color':'black','background-color':'rgba(255,255,255,0.2)'}))))),

  $('.peastock').append(`
    <div id="ree">ES6 isnt suitable for all of jQuery's usage!</div>
    <div>Since some functions inside of jQuery's methods dont require 'this' or 'return'</div>
    <div>You can learn this by going <a href="https://www.w3schools.com/Js/js_es6.asp">here</a></div>
  `),
  $("*").css({"margin":"0 auto","padding":"1%"}),
  $("* .peastock, .peastock, .peastock *").css({"background-color":"rgba(128,0,0,0.1)"})
);

Source: jQuery .each() The fiddle in action (with updates!)

to truely answer the question with an added use
Source Link

Going to officially answer your question: with a one-liner:

//If this is only one use variable you can use
$(['#p1','#p2','#p3','#p4','#p5'].join(',')).methodToUse();
//if you DO need it as a variable you can
var joined = ['#p1','#p2','#p3','#p4','#p5'].join(',');
$(joined).methodsToUse();

If you want them to do something individually there is also .each();

Going to officially answer your question: with a one-liner:

//If this is only one use variable you can use
$(['#p1','#p2','#p3','#p4','#p5'].join(',')).methodToUse();
//if you DO need it as a variable you can
var joined = ['#p1','#p2','#p3','#p4','#p5'].join(',');
$(joined).methodsToUse();

If you want them to do something individually there is also .each();

Going to officially answer your question: with a one-liner:

//If this is only one use variable you can use
$(['#p1','#p2','#p3','#p4','#p5'].join(',')).methodToUse();
//if you DO need it as a variable you can
var joined = ['#p1','#p2','#p3','#p4','#p5'].join(',');
$(joined).methodsToUse();

Going to officially answer your question: with a one-liner:

//If this is only one use variable you can use
$(['#p1','#p2','#p3','#p4','#p5'].join(',')).methodToUse();
//if you DO need it as a variable you can
var joined = ['#p1','#p2','#p3','#p4','#p5'].join(',');
$(joined).methodsToUse();

If you want them to do something individually there is also .each();

to officially answer the OP's question
Source Link

If you want them to do something individually there is also .each();

In the example below, each p ids clicks makes any one of them red:

var peas = ['#p1','#p2','#p3','#p4','#p5'];
$.each(peas, function(i){
    $(peas[i]).click( function(){
        $(peas[i]).css({'color':'red'});
    });
});

When you throw 'i' into a function parameter, it finds the values inside the arrays appropriately. When you do '.each()' the format looks like this:

$.each(array, function(i){
    // any code you wish as long as you have an array selector
    //$(array[i]).whatever function
});

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 an array of nonPea and voila!

var peas = ['#p1','#p2','#p3','#p4','#p5']
,   nonPeas = ['#p5, #p2, #p3, #p4'
              ,'#p1, #p5, #p3, #p4'
              ,'#p1, #p2, #p5, #p4'
              ,'#p1, #p2, #p3, #p5'
              ,'#p1, #p2, #p3, #p4']
;
$.each(peas, function(i){
    $(peas[i]).click( function(){
        $(peas[i]).css({'color':'red'});
        $(nonPeas[i]).css({'color':'black'});
    });
});

I know someone is bound to want to know about each value array as jquery selectors. Hope everything goes well!

Going to officially answer your question: with a one-liner:

//If this is only one use variable you can use
$(['#p1','#p2','#p3','#p4','#p5'].join(',')).methodToUse();
//if you DO need it as a variable you can
var joined = ['#p1','#p2','#p3','#p4','#p5'].join(',');
$(joined).methodsToUse();

Source: jQuery .each() The fiddle in action (with updates!)

If you want them to do something individually there is also .each();

In the example below, each p ids clicks makes any one of them red:

var peas = ['#p1','#p2','#p3','#p4','#p5'];
$.each(peas, function(i){
    $(peas[i]).click( function(){
        $(peas[i]).css({'color':'red'});
    });
});

When you throw 'i' into a function parameter, it finds the values inside the arrays appropriately. When you do '.each()' the format looks like this:

$.each(array, function(i){
    // any code you wish as long as you have an array selector
    //$(array[i]).whatever function
});

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 an array of nonPea and voila!

var peas = ['#p1','#p2','#p3','#p4','#p5']
,   nonPeas = ['#p5, #p2, #p3, #p4'
              ,'#p1, #p5, #p3, #p4'
              ,'#p1, #p2, #p5, #p4'
              ,'#p1, #p2, #p3, #p5'
              ,'#p1, #p2, #p3, #p4']
;
$.each(peas, function(i){
    $(peas[i]).click( function(){
        $(peas[i]).css({'color':'red'});
        $(nonPeas[i]).css({'color':'black'});
    });
});

I know someone is bound to want to know about each value array as jquery selectors. Hope everything goes well!

Source: jQuery .each() The fiddle in action (with updates!)

If you want them to do something individually there is also .each();

In the example below, each p ids clicks makes any one of them red:

var peas = ['#p1','#p2','#p3','#p4','#p5'];
$.each(peas, function(i){
    $(peas[i]).click( function(){
        $(peas[i]).css({'color':'red'});
    });
});

When you throw 'i' into a function parameter, it finds the values inside the arrays appropriately. When you do '.each()' the format looks like this:

$.each(array, function(i){
    // any code you wish as long as you have an array selector
    //$(array[i]).whatever function
});

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 an array of nonPea and voila!

var peas = ['#p1','#p2','#p3','#p4','#p5']
,   nonPeas = ['#p5, #p2, #p3, #p4'
              ,'#p1, #p5, #p3, #p4'
              ,'#p1, #p2, #p5, #p4'
              ,'#p1, #p2, #p3, #p5'
              ,'#p1, #p2, #p3, #p4']
;
$.each(peas, function(i){
    $(peas[i]).click( function(){
        $(peas[i]).css({'color':'red'});
        $(nonPeas[i]).css({'color':'black'});
    });
});

I know someone is bound to want to know about each value array as jquery selectors. Hope everything goes well!

Going to officially answer your question: with a one-liner:

//If this is only one use variable you can use
$(['#p1','#p2','#p3','#p4','#p5'].join(',')).methodToUse();
//if you DO need it as a variable you can
var joined = ['#p1','#p2','#p3','#p4','#p5'].join(',');
$(joined).methodsToUse();

Source: jQuery .each() The fiddle in action (with updates!)

added 149 characters in body
Source Link
Loading
added 6 characters in body
Source Link
Loading
Source Link
Loading