tags:

views:

43

answers:

1
function test() {
    var foo = [];
    $('#tree table').each(function(i, table) {
        foo[i] = $(table).text().trim();
    });

    var ch = 'G';

    for (j = 0; j <= 12; j++) {
        if (foo[j] ^= ch) {
            alert(foo[j]);
        }
    }
}

[foo[j] ^= ch] startsWith selector isn't working in the above code. Help needed. Couldn't find any answer. Thanks in adv.

+2  A: 

The starts-with selector is for use within jQuery selectors when selecting DOM elements based on the value of an attribute.

This does not work within regular javascript if() statements.

Try this instead:

var ch = 'G';
if ( foo[j].indexOf(ch) === 0 ) {
    alert( foo[j] );
}

This will check foo[j] to see if the index position (if any) of the value of the ch variable is position 0 (in other words, at the beginning).


EDIT:

Another alternative would be to specifically test the first character against ch. But this will only work for testing one character. If ch contains more than one, it will fail.

var ch = 'G';
if ( foo[j].charAt( 0 ) === ch ) {
    alert( foo[j] );
}
patrick dw
It works. Thank you.
Bala
@Bala - You're welcome. :o)
patrick dw
@Bala - If this answer was helpful, please remember to "Accept" it by clicking the large checkmark to the left of it. Thanks. :o)
patrick dw
@ patrick dw - i am new here.And yes, i accepted it now. Thanks.
Bala