diff --git a/app/javascript/new_design/champs/siret.js b/app/javascript/new_design/champs/siret.js index 22e814851..9b3e29a75 100644 --- a/app/javascript/new_design/champs/siret.js +++ b/app/javascript/new_design/champs/siret.js @@ -1,37 +1,35 @@ import $ from 'jquery'; -addEventListener('turbolinks:load', () => { - $('[data-siret]').on('input', evt => { - const input = $(evt.target); - const value = input.val(); - const url = input.attr('data-siret'); +$(document).on('input', '[data-siret]', evt => { + const input = $(evt.target); + const value = input.val(); + const url = input.data('siret'); - switch (value.length) { - case 0: - input.removeAttr('data-invalid'); - $.get(`${url}?siret=blank`); - break; - case 14: - input.attr('disabled', 'disabled'); - $('.spinner').show(); - $.get(`${url}?siret=${value}`).then( - () => { - input.removeAttr('data-invalid'); - input.removeAttr('disabled'); - $('.spinner').hide(); - }, - () => { - input.removeAttr('disabled'); - input.attr('data-invalid', true); - $('.spinner').hide(); - } - ); - break; - default: - if (!input.attr('data-invalid')) { - input.attr('data-invalid', true); - $.get(`${url}?siret=invalid`); + switch (value.length) { + case 0: + input.removeData('invalid'); + $.get(url, { siret: 'blank' }); + break; + case 14: + input.attr('disabled', true); + $('.spinner').show(); + $.get(url, { siret: value }).then( + () => { + input.removeData('invalid'); + input.removeAttr('disabled'); + $('.spinner').hide(); + }, + () => { + input.removeAttr('disabled'); + input.data('invalid', true); + $('.spinner').hide(); } - } - }); + ); + break; + default: + if (!input.data('invalid')) { + input.data('invalid', true); + $.get(url, { siret: 'invalid' }); + } + } });