Jump to content
MakeWebGames

Counting Classes with JQuery


Coly010

Recommended Posts

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:

 

  1. is there a way for me to create an array of all the elements that contain this specific class
  2. then iterate through this array
  3. 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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...