Lau Lau - 1 year ago 103
Javascript Question

Add object property from input.value

Say I have an object with states as properties and an html input text field.

<input id="input" type="text">

var foo = {
"Wisconsin" : "Madison",
"Illinois" : "Springfield"


How can I add properties to foo using the input text field ?
I tried this :

foo[input.value] = input.value;

but of course it didn't work...

Thanks for your help

Answer Source
foo.newValue = input.value

You are dealing with an object not an array.

So this will result in

{ "Wisconsin" : "Madison", "Illinois" : "Springfield", "newValue" : VALUE-OF-INPUT-FIELD


Here is a working fiddle

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download