views:

99

answers:

3

OK, so let's say I have this:

$(function() {
  $('#good_evening').keyup(function () {
    switch($(this).val()) {
    case 'Test':
      // DO STUFF HERE
      break;
    }
  });
});

... this would only run if you typed "Test" and not "test" or "TEST". How do I make it case-insensitive for JavaScript functions?

+6  A: 
switch($(this).val().toLowerCase()) {
case 'test':
// DO STUFF HERE
break;
Matt Briggs
Ah yes -- brilliant yet simple solution. Thanks.
Dan
2mins faster than me. *slaps self* for editing tags before answering the question :)
Marko
@dan: glad to have helped :) @Marko: can't count the amount of times I've deleted an answer and up voted someone else ;-)
Matt Briggs
+2  A: 

Convert it to upper case. I believe this is how it is done, correct me if I am wrong... (dont -1 me =D )

$(function() {
$('#good_evening').keyup(function () {
switch($(this).val().toUpperCase()) {
case 'TEST':
// DO STUFF HERE
break;
}
});
});
Cipi
Correct! But Matt was first. Sorry. :P
Dan
Ok... so accept one answer... :P
Cipi
+2  A: 

Why not lowercase the value, and check against lowercase inside your switch statement?

$(function() {
    $('#good_evening').keyup(function () {
        switch($(this).val().toLowerCase()) {
        case 'test':
        // DO STUFF HERE
        break;
        }
    });
});
Marko
Great minds think alike. Check Matt's answer. :P
Dan