0

I'm trying to run some functions after using val(). I know it doesn't have a callback so I'm using the change() method as I have read (here) but I still can't get it running.

$(id).addClass('Editing').append('<option selected value="' + CodProduto + '">' + CodProduto + ' - ' + ProdutoDesignacao + '</option>').val(CodProduto).change(function() {
  id = '#CodLocalizacao' + CodArmazem;
  $(id).append('<option selected value="' + CodLocalizacao + '">' + CodLocalizacao + ' - ' + LocalizacaoDesignacao + '</option>').val(CodLocalizacao).change(function() {
    //LOTE
    if (CodLote) {
      $('.div_CodLote').show();
      id = '#CodLote' + CodArmazem;
      $(id).append('<option selected value="' + CodLote + '">' + CodLote + ' - ' + LoteDesignacao + '</option>').val(CodLote);
    } else {
      $('.div_CodLote').hide();
    }
  }); //#second callback
}); //#first callback
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Alexis Garcia
  • 452
  • 3
  • 15
  • Can you describe the goal you're trying to reach with this code? I'm certain there's a more effective way to achieve it. – Rory McCrossan Jan 16 '20 at 16:16
  • You shouldn't attach `change` listener to the option elements, it should be attached to the select element instead. – Teemu Jan 16 '20 at 16:17
  • It is attached to the select,````$(id).append```` is the select not the option – Alexis Garcia Jan 16 '20 at 16:26
  • Like i said, im trying to run a function after the .val(), after i set the val of an input i want to run a function – Alexis Garcia Jan 16 '20 at 16:54
  • `CodArmazem` probably won't be what you're expecting inside the change event as it may change - see https://stackoverflow.com/questions/111102/how-do-javascript-closures-work – freedomn-m Jan 16 '20 at 17:13
  • You need to trigger the change event after setting the val: `.val(x).change();` if you want to trigger change when you change via code. – freedomn-m Jan 16 '20 at 17:16

0 Answers0