namretiolnave
namretiolnave

Reputation: 801

incrementing eq() with jquery

Somewhat related to what what is being discussed here: jQuery eq() loop

I have any number of elements that can be added to a page:

instead of doing this:

$('.taskName a:eq(1)').addClass('fixme');
$('.taskName a:eq(2)').addClass('fixme');

//can I programatically add more here as items get added instead of having to code 1,2,3,4 etc?

I would like to have a count done and have the number inside the eq() be incremented as a new element gets added.

I got around the other problems so I removed any code that is no longer needed for this question.

Upvotes: 0

Views: 166

Answers (2)

Explosion Pills
Explosion Pills

Reputation: 191779

It's hard to tell exactly which elements need .fixme since you didn't post your html, but I'll assume it's all but the first and last elements. In that case, this will do the job:

$('.taskName a').slice(1, -1).addClass('fixme');

.slice

Upvotes: 1

adeneo
adeneo

Reputation: 318302

Not sure I get it, but I'll try :

$('a').filter(function() {
   return $(this).closest('.taskName').attr('title') == '' || 
          $(this).closest('.taskName').attr('title') == 'Role: Repeter_Manager';
}.addClass('fixme');

Adds the class to anchors inside elements with the matching titles ???

Upvotes: 1

Related Questions