Trying to practice jQuery on jsfiddle. I'm trying to count how many letters are in the div element. What am I doing wrong (I'm sure a LOT of things) and am I using the right methods?
Thanks in advance :)
edit: sorry, i wasn't trying to really how many letters in the string, but how many of the letter i'm want to query, is in the div element. i guess if i want to count the letters i would just use .length, but shouldn't I use a loop to go through the string and see if the letter I'm looking for is in the string?
HTML
<div>Hello!</div>
jQuery
$(document).ready(function() {
var countLetters = function (letter) {
var counter = 0;
for (i = 0; i <= $('div').length; i++) {
if ($('div')[i] == letter) {
counter++;
$('div').append('<p>There are ' + counter + ' ' + letter + 's</p>');
} else {
$('div').append('<p>That letter isn\'t found</p>');
}
}
}
countLetters('e');
});
$('div')[i]is a dom element reference... so that will never be equal toletterdivand count the letters