JavaScript Program to Count Vowels in a Given String

Write a JavaScript function to count the number of vowels in a given string.



Source Code
<html>
<head>
    <title>Count vowels in given string</title>
<script>
    function countVowels()
    {
        var i=0, count=0;
        var ch;
        var txt = document.getElementById('txt').value;

        for(var i=0; i<txt.length; i++)
        {
            ch = txt.charAt(i);
            if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || 
                ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
            {
                count++;
            }
        }
        document.getElementById('panel').innerHTML = "No. of vowels in given string: " + count;
    }
</script>
</head>
<body>
Enter some text<br/>
<textarea rows="2" cols="20" id="txt"></textarea><br/>
<input type="button" value="COUNT VOWELS" onclick="countVowels()"/>
<p id="panel">No. of vowels in given string: 0</p>
</body>
</html>
Output