Thanks for reading.
if we have:
var letters = ‘a b c h’; // …
how can we get this result:
<div class="box">
<div class="letter">a</div>
<div class="letter">b</div>
<div class="letter">c</div>
<div class="letter">h</div>
<!-- ... -->
</div>
(In DOM ready) something like:
var letters = 'a b c h k w x'; // 7 letters (can be more...)
var char = letters.split(' ');
var nOfChars = char.length;
var divLetter = $('<div class="letter" />');
for(var char = 0; char < nOfChars; ++char) {
$('.box').append( divLetter );
$('divLetter').html( char );
}
(Far from good, I know. please help)
demo
Broken down, it’s the following:
.appendToto attach it to DOM. You can place this anywhere, or have the div already on the page–up to you..each()method applied to the result of theString.split(which results in an array of letters). Each then begins iterating over each unique letterappendToto attach it to the original divAll said and done, we have the result you’re looking for.