Reputation: 11557
Let's say, i have two input fields, their values: val1 and val2. (they are strings)
How do i convert them into associative array: {val1:val2} first value as key, second one as value??
Thanks in advance!
Upvotes: 0
Views: 187
Reputation: 339786
// get the key and the value
var key = document.getElementById('input1').value;
var val = document.getElementById('input2').value;
// and store 'em
var obj = {}; // need an empty object first, as you
obj[key] = val; // can't store a property on a null value
Upvotes: 2
Reputation: 943193
var foo = {};
foo[document.getElementById('an_input').value] = document.getElementById('another_input').value
Upvotes: 2