javascript - How to properly reset an object item -


i'm working on requires me put value of input field object item.

var test = {     item1:{         subitem1: "", <------- 1 i'm trying fill         subitem2: "",         subitem3: "",         subitem4: ""     } }; 

this value gets filled when input field #thecontent changes

$('.wrapper').on('change', '#thecontent', function(){     var thecontent = $(this).val();     test.item1.subitem1 = thecontent; }); 

now, input text field visible after check input checkbox #checkit

if(this.checked) {     $('#thecontent').show(); } 

if checkbox unchecked however, not input field disapear, object item needs reset. i'm doing following:

else{     $('#thecontent').hide();             //this part needs reset in different way     test.item1.subitem1 = ""; } 

this works, however, if try fill object item again after reset once, doesn't fill new value anymore.

how go resetting value inside object can fill again later on?

to conclude:

  • check box ->
  • enter "this value" in input box ->
  • click "test it" ->
  • subitem1 populated "this value" in console ->
  • uncheck box reset object ->
  • check box again ->
  • enter "this different value" ->
  • click "test it" ->
  • see empty subitem1 while expected "this different value" in console.

what did miss?

fiddle

if don't intend on removing text input when hiding it, 1 solution trigger "change" programmatically:

if(this.checked) {     $('#thecontent').show().trigger("change"); } 

see updated fiddle: http://jsfiddle.net/2dqmkvrr/4/


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -