Johnatan De Leon
Johnatan De Leon

Reputation: 166

Pass a variable to Jquery's parameter as if it was a class

$('.reporte, .maquinaria').on('change', function () {
    var clase = $(this).hasClass('reporte') ? "reporte" : "maquinaria";
    var item = {};

    $(clase).each(function () {
        item[$(this).attr('id')] = $(this).val();
    });
    json[clase].splice(0, 1);
    json[clase].push({
        item
    });
    console.log(json[clase])
});

if I make something like this:

$('.clase').each(function () {
    item[$(this).attr('id')] = $(this).val();
});

it works. But I want to pass the class with the var clase that I already have.

Upvotes: 0

Views: 36

Answers (1)

Barmar
Barmar

Reputation: 781974

The class needs a . before it in the selector. $("." + clase)

Upvotes: 2

Related Questions