Задать вопрос
  • Как сделать, чтобы выбранное значение в списке заменяло собой список?

    0xD34F
    @0xD34F Куратор тега JavaScript
    $('table').on('change', 'select', ({ target: t }) => {
    
      $(t).replaceWith(t.value);
    
      // или
    
      $(t).prop('outerText', t.value);
    
      // или
    
      $(t).after(t.value).remove();
    });

    или

    const isSelect = el => el.tagName === 'SELECT';
    // или
    const isSelect = el => el.nodeName === 'SELECT';
    // или
    const isSelect = el => el.matches('select');
    // или
    const isSelect = el => el instanceof HTMLSelectElement;

    document.querySelector('table').addEventListener('change', ({ target: t }) => {
      if (isSelect(t)) {
    
        t.replaceWith(t.value);
    
        // или
    
        t.parentNode.replaceChild(new Text(t.value), t);
    
        // или
    
        t.outerText = t.value;
    
        // или
    
        t.after(t.value);
        t.remove();
    
        // или
    
        t.parentNode.replaceChildren(...Array.from(
          t.parentNode.childNodes,
          n => n === t ? t.value : n
        ));
      }
    });
    Ответ написан
    Комментировать