Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Number" divs with the Alphabet using jQuery .each()

I use this to number divs that have the class "number":

$("#parent .number").each(function(i){
    $(this).html((i+1) + ". ");
});

but how would I use something similar to instead "number" as a,b,c,d etc?

like image 815
Atom Vayalinkal Avatar asked Jan 30 '26 08:01

Atom Vayalinkal


2 Answers

Use string.fromCharCode:

$("#parent .number").each(function(i){
    $(this).html(String.fromCharCode(97 + i) + ". ");
});

Also, if you would like to use capitalized characters, then use 65 instead of 97.

like image 87
nullable Avatar answered Feb 01 '26 21:02

nullable


You can create an array of alphabet characters:

var alpha = ["a","b","c",....];

then use the index to print them:

$("#parent .number").each(function(i){
    $(this).html(alpha[i] + ". ");
    });

Just so you know, you can use an ordered list to do this using html and css:

The html:

<ol class="alpha">
   <li>text ...</li>
   <li>text ...</li>
   <li>text ...</li>
   <li>text ...</li>
</ol>

the css:

ol.alpha li {
   list-style-type:lower-alpha;
}
like image 23
Ibu Avatar answered Feb 01 '26 22:02

Ibu