Skip to content
Advertisement

assign object keys dynamically using jquery or vanilla js [closed]

I have an empty object in which I would like to assign data dynamically

Expected results

userData = {
 programmer: "Jeff",
 designer: "Obama',
 CEO: "Elon Musk"
}

What I have tried so far.

var userData = {};
var allData = [];

$('input[type="text"]').on("input", function (e) {
    var getBtn = $("#text-label").attr('text-label');
    var inputValue = e.target.value;
    userData.getBtn = inputValue;
    allData.push(userData)
     console.log(allData)
})

On console I see

[{
    getBtn: "Jim"
}]

but the expected result should be

[{
        designer: "Jim"
}]

What do I need to change to get what I want ?

Advertisement

Answer

userData[getBtn] = inputValue;

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement