javascript - How to create an array from .each loop with jQuery -
how can create array inside of '.each loop' , use outside of loop?
my .each loop
:
// loop through button class .apply $('.profile-nav ul li a').not('.apply').each( function() { // if loop through element has .cur class if( $(this).hasclass('cur') ) { //get first class of match element var classestoapply = $(this).prop('class').split(' ')[0]; } //how can create array classestoapply? //var arr = jquery.makearray(classestoapply); // create array, 1 element });
how can create array var = classestoapply
?
and how can array? e.g
$( allclasses array selectors).dostuff();
if declare variable outside of each
, accessible inside each
:
var yourarray = []; $('.profile-nav ul li a').not('.apply').each(function() { if($(this).hasclass('cur')) { yourarray.push($(this).prop('class').split(' ')[0]); } }); //here, yourarray contain strings require.
although others have shown, there ways shorten code significantly.
Comments
Post a Comment