Coly010 Posted November 26, 2013 Share Posted November 26, 2013 I know I can count how many elements there are containing the same class in jquery using: $(".classname").length however what i'd like to know is : is there a way for me to change each of the elements containing the class individually, all at once? For example: is there a way for me to create an array of all the elements that contain this specific class then iterate through this array so that i can dynamically change certain variables depending on a variable from the previous element that i changed it would be extremely helpful if someone could show me how to do this. Colum Quote Link to comment Share on other sites More sharing options...
Dave Posted November 26, 2013 Share Posted November 26, 2013 Sure it's easy $('.classname').each(function() { $(this).addClass('test'); }); Quote Link to comment Share on other sites More sharing options...
Coly010 Posted November 27, 2013 Author Share Posted November 27, 2013 Ok so for me to do what I want to do, I can simply place a variable say: var add = 0; Then use the each() to dynamically update each class, for example: $(".classname").each(function() { add = add + $("#someDiv").outerWidth(); $(this).css({ left: add }); }); Would that work? Will the add variable change everytime it goes to the next element? Quote Link to comment Share on other sites More sharing options...
Dave Posted November 27, 2013 Share Posted November 27, 2013 Ok so for me to do what I want to do, I can simply place a variable say: var add = 0; Then use the each() to dynamically update each class, for example: $(".classname").each(function() { add = add + $("#someDiv").outerWidth(); $(this).css({ left: add }); }); Would that work? Will the add variable change everytime it goes to the next element? In theory yes. Why not test it and report back? Quote Link to comment Share on other sites More sharing options...
Coly010 Posted November 27, 2013 Author Share Posted November 27, 2013 Can't at the moment, I'm in school aha Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.