views:

41

answers:

3

Hello,

I wondering if it is possible to count the number of elements using jquery and then using that prefix the element with a number, so for example I have 2 selects on a page, is it possible to count the selects and then give the selects, a class e.g.,

<select class="1"> <select class="2">

+3  A: 
$("select").each(function(i, sel) {
    $(sel).addClass("class-" + i);
}

Edit: forgot classes can't start with a number.

Ian Wetherbee
Classes can only start with a letter I think...
Squ36
I would make this something like `addClass( 'c'+i )` since it's not legal to start a class name with a number.
Ken Redler
+1  A: 
$('select').each(function(index) {
    $(this).addClass('prefix_' + (index + 1));
});

Note that a class name cannot start with a number so make sure the class name starts with a letter and then put the index.

Darin Dimitrov
+4  A: 

Hmm, something like:

$("select").each( function (i) {
   $(this).addClass( "select_" + i ); //Classes can't start with a number.
});
Jake