G
G
Genri_Rus2021-06-23 23:28:41
JavaScript
Genri_Rus, 2021-06-23 23:28:41

Why are inputs grouped incorrectly?

Here is an example: https://codepen.io/Genri_Rus/pen/YzZoYWK

Now when you click on the group button, the corresponding fields appear below.

But why are they grouped a little wrong?

If you select one group, then everything is fine, but if there are several, then the arrNameInput array adds incorrectly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-06-24
@Genri_Rus

$('.form').on('submit', function (e) {
  e.preventDefault();
  const values = {};
  
  $('.form__check').each(function () {
    if ($(this).is(':checked')) {
      const [name, ...keys] = $(this).attr('name').split('-');
      if (name) {
        (values[name] ??= []).push(keys.join('-'));
      }
    }
  });
  
  for (const key in values) {
    $(`.inputs__value--${key}`).text(values[key].join(','));
  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question