Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

assign object keys dynamically using jquery or vanilla js

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?

>Solution :

userData[getBtn] = inputValue;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading