DrStrangeLove
DrStrangeLove

Reputation: 11557

input fields associative array

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

Answers (2)

Alnitak
Alnitak

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

Quentin
Quentin

Reputation: 943193

var foo = {};
foo[document.getElementById('an_input').value] = document.getElementById('another_input').value

Upvotes: 2

Related Questions