Mac
Mac

Reputation: 7533

Displaying english alphabets in a div

for (var a = 0; a < 26; a++) {
                var alpha = $('<span class="alpha" />');
                alpha.addClass('alpha-' + (a % 2) + 1);
                $('.alphabets').append(alpha);
                $('.alpha').text(String.fromCharCode(65 + a));
            }

i am doing something like this and i know i am making mistake in

$('.alpha').text(String.fromCharCode(65 + a));

thats why i am getting "Z" in every span but i am not getting how to fix this

Upvotes: 0

Views: 241

Answers (3)

Capsule
Capsule

Reputation: 6159

   $('.alphabets').append(alpha);
   $('.alpha').text(String.fromCharCode(65 + a));

Should be

   alpha.text(String.fromCharCode(65 + a));
   $('.alphabets').append(alpha);

Upvotes: 0

Ivo Wetzel
Ivo Wetzel

Reputation: 46745

Your code overwrites the text of all <span>'s that happen to have the alpha class.

 $('.alpha').text(String.fromCharCode(65 + a));

You need to change your code so that it only alters the text of the last created <span>:

 alpha.text(String.fromCharCode(65 + a)); 

Upvotes: 1

Quentin
Quentin

Reputation: 943547

Change $('.alpha') (every element that is a member of the class alpha) to alpha (the specific element you just created and stored in that variable)

Upvotes: 4

Related Questions