LoLo LoLo - 1 year ago 89
jQuery Question

Print only vowels from string from textbox

I want to use two for loops and use

so if I put in "Banana" in the text box it will only show "aaa"

= textbox

= label (where the result will show)

This is what I have, and it's not working correctly...

$("#vow").click(function () {
var vowels = "aeiou";
var v = $("#textbox").val();
var label = $("#lbl").html(v);

for (i = 0; i < vowels.length; i++) {
vowels.charAt(i) == v.charAt(j);
for (j = 0; j < v.length; j++) {
label = v;

Answer Source

"Simple" code (at least to read):

var vowels = "aeiou";
var label = "banana";

var stripped = "";
for (i = 0; i < label.length; i++) {
  if(vowels.indexOf(label[i]) >= 0) stripped += label[i];

But I advice you to simply use :

"banana".replace(/[^aeiouy]/ig, "")

I is a regex that says: "replace everything that is not one of aeiouy to nothing". i and g are flags: i = case insensitive, g = global (replace all occurrences)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download