user1552172
user1552172

Reputation: 676

javascript switch statement not firing

        var input = $('#csearchtext').val();
    var dropdownindex = $("select[name='columnlist'] option:selected").index();
    var radioselected = $("input:radio[name='group2']:checked").val();
    var searchpattern = '';

    switch (radioselected) {
        case 1:
            searchpattern = '/^'+input+'/'+'ig';
            break;
        case 2:
            searchpattern = input;
            break;
        case 3:
            searchpattern = '/'+input+'$/'+'ig';
            break;
    }


    console.log(searchpattern);

    $('#table_id').dataTable().fnFilter(searchpattern, dropdownindex + 1, false, true, true, false);

Input is working fine, as-well as dropdown and radio selecting. Radioselected is either 1,2, or 3 and 2 is the default checked, though with the console.log searchpattern is still empty yet the switch statement should go into effect every time.

Upvotes: 0

Views: 74

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074989

val always returns a string or undefined. switch works with strict equality (e.g., the same as ===, not ==), so either use parseInt on radioselected or use "1" and such as the cases.

Gratuitous live example | source

Upvotes: 9

Related Questions