javascript - Add object property from input.value - Stack Overflow

Say I have an object with states as properties and an html input text field.<input id="input&qu

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

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

Share Improve this question edited Sep 12, 2016 at 18:18 Lau asked Sep 12, 2016 at 18:14 LauLau 1792 gold badges4 silver badges13 bronze badges 1
  • Try to add a button or at least a change listener to monitor change in input and then update foo by getting new value entered in text box. – Harry Joy Commented Sep 12, 2016 at 18:17
Add a ment  | 

2 Answers 2

Reset to default 1

To use normal javascript functions you can use:

foo.input = document.getElementById('input').value;

Or you can use jQuery:

foo.input = $('#input').val();

Input Box Value Property: http://www.w3schools./jsref/prop_text_value.asp

jQuery .val(): http://api.jquery./val/

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745261187a4619218.html

相关推荐

  • javascript - Add object property from input.value - Stack Overflow

    Say I have an object with states as properties and an html input text field.<input id="input&qu

    3小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信