jeudi 13 août 2015

jQuery selector from a variable

JSFIDDLE LINK

var $select = $('select');

$select.on('change', function () {
    var value = $(this).val();
    var modifier = value.split('_')[0];
    var target = modifier + '_target';

    console.log(target);

    $('div').filter(target).addClass('active');

});

If you open console and select any option from the select, you'll see that the text that is thrown to the console is legit (as I want to select modifier1_target depending on the selected option).

However, I can not make a jQuery selector out of this, so as the div is selected and applied an active class.

I tried $(el).attr('class', target) but it didn't work surely.

I am running out of ideas where am I wrong here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire