core/jQuery.once is being deprecated in core.
https://www.drupal.org/node/3158256
1 Need help rewriting some lines of code using the core/once library.

Old:

  
$('.hub-recettes').find('input').once('recomandation-filter').on('ifChecked ifUnchecked', function (event) {
      $('#views-exposed-form-hub-recette-page-1').submit();
    });

New:

const filterElements = $(once('recommandation-filter', $('.hub-recettes').find('input:checkbox')));
          filterElements.on('ifChecked ifUnchecked', function (event) {
        $('#views-exposed-form-hub-recette-page-1').submit();
      });

The new code is not working when the checkbox is checked or unchecked. I can't seem to find a solution to this.
Checkboxes on this page is transformed using the icheck library.
Any ideas ?

Comments

jaypan’s picture

I hadn't realized they deprecate $.once(), so my comment below is an educated guess.

It looks like this:

  const filterElements = $(once('recommandation-filter', $('.hub-recettes').find('input:checkbox')));

Is supposed to be this:

  const filterElements = once('recommandation-filter', '.hub-recettes input:checkbox');

Contact me to contract me for D7 -> D10/11 migrations.

j.b’s picture

Thank you Jaypan,

Final code:
    const filterElements = once('recommandation-filter', '.hub-recettes');
     $(filterElements).on('ifChecked ifUnchecked', 'input:checkbox', function (event) {
        $('#views-exposed-form-hub-recette-page-1').submit();
      });

drupaldope’s picture

hmm.. I recently wrote some code using .once too

 $('div.mydiv').once('myotherdiv').each(function () {

can it simply be replaced with .. err...

const mystuff = once('myotherdiv', 'div.mydiv', context);
$(mystuff).each(function (event) {

is that correct?