0

So I'me fairly new to coding Jquery. I wanted to load dynamically all letters of the alphabet inside a container.

Currently I got it build up like this:

<div class="letters">
    <div>A</div>
    ...
    <div>Z</div>
</div>

I would like to load each letter inside of the .letters with a div tag.

How would you do this?

Thnx for the help in advance,

hashtagrik
  • 23
  • 4
  • You are actually meant to make an attempt yourself and then show the code where you are having problems – Pete Sep 01 '16 at 10:15
  • What do you want? To put every letter in (as the title says) or get every letter out (like the post says)? What are the inputs and outputs we're talking about? – Whothehellisthat Sep 01 '16 at 10:18

5 Answers5

1

Here is how i would make it:

function genCharArray(charA, charZ) {
    var a = [], i = charA.charCodeAt(0), j = charZ.charCodeAt(0);
    for (; i <= j; ++i) {
        a.push(String.fromCharCode(i));
    }
    return a;
}

var array = genCharArray('a', 'z');

for(var i=0; i<array.length; i++) {
  $('#alphabet').append('<div>' + array[i] + '</div>')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="alphabet"></div>

I build a function to generate an array of the alphabet (so you can chose if you want the full alphabet or only from a certain letter to another, or you can also chose to use the capitalize alphabet by using genCharArray('A', 'Z') and then use a for loop to iterate it and generate the divs.

0x01
  • 885
  • 7
  • 17
1

Try this :

$(document).ready(function(){
    for(var i = 0; i < 26; i++) {
        var char = String.fromCharCode(65+i);
        $("#letters").append("<div>"+char+"</div>");
    }
});

Use 65 to get Capital letters, 97 for lowercase.

Kevin Grosgojat
  • 1,386
  • 8
  • 13
0
for (var i = 65; i <= 90; i++) {
    $('.letters').append('<div>' + String.fromCharCode(i) + '</div>');
}
0

You could iterate the ASCII codes of the alphabet letters (which are between 65 and 90 if you need capital letters), then create a div, the text of which contains the corresponding alphabet letter (by using the JavaScript String.fromCharCode function), and finally append it to .letters (with the jQuery appendTo method).

for (i = 65; i <= 90; i++) {
  $("<div></div>").text(String.fromCharCode(i)).appendTo(".letters");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="letters"></div>
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
0

Javascript code (using jQuery)

// Let's build a function for building the alphabet array
function getAlphabet(first, last) {
    var alphabet = [];
  
  for (i = first.charCodeAt(0); i <= last.charCodeAt(0); ++i) {
    alphabet.push(String.fromCharCode(i));
    }
    
  return alphabet;
}

// Calling the function
var alphabet = getAlphabet('A', 'Z'); // ["a", ..., "z"]

// Printing the array inside the .letters element
$.each(alphabet, function (index, element) {
    $(".letters").append("<div>" + element + "</div>");
});

Try it on JSFiddle.

Explanation

I have made a function that is able to build an array containing the alphabet. Then, we store that in a variable and, finally, we use jQuery .each() method that allows us to go throw the array and, inside each iteration, we can use .append() in order to add the letters inside the div.

Community
  • 1
  • 1