Delete <dt> without <dd> using jquery

I have a list of definitions and I need to remove all tags <dt>that do not have <dd> In this particular case: Herramientas, Suplementos, Repuestos, Herramientas and Anti pinchaduras

The list can vary greatly since it depends on stocks (any category <dt>may be empty)

I tried this

$('dt+dt').each(function() {
    $(this).remove();
});

But it removes "partes" instead of "herramientas" and does not delete "suplementos", Also tried using :empty, but it seems that dt without dd is not considered empty ...

There should be a very simple and simple solution, but my brain does not see it: - (

List example:

<dl>
    <dt>Bicicletas</dt>
        <dd><a href="/bicicletas/">Bicicletas</a> (70)</dd>
    <dt>Accesorios</dt>
        <dd><a href="/accesorios/bocinas/">Bocinas, timbres y cornetas</a> (9)</dd>
        <dd><a href="/accesorios/transporte/">Transporte y protección</a> (1)</dd>
    <dt>Herramientas</dt>
    <dt>Partes</dt>
        <dd><a href="/partes/cubiertas/">Cubiertas</a> (2)</dd>
        <dd><a href="/partes/asientos/">Asientos</a> (5)</dd>
        <dd><a href="/partes/grips/">Puños / grips</a> (1)</dd>
    <dt>Articulos de indumentaria</dt>
        <dd><a href="/indumentaria/jerseys/">Jerseys / Remeras</a> (1)</dd>
        <dd><a href="/indumentaria/cascos/">Cascos</a> (3)</dd>
    <dt>Suplementos</dt>
    <dt>Repuestos</dt>
    <dt>Herramientas</dt>
    <dt>Anti pinchaduras</dt>
</dl>
+3
4
$('dt').filter(function(){
                     return !($(this).next().is('dd'));
               }).remove();

: http://jsbin.com/isute


, <dt>, anothe selector:

$('dt + dt').prev().add('dt:last-child').remove();

: <dt>, <dt>, prev dt .


:

var good = $('dd').prev('dt');
$('dt').not(good).remove();

not , . $ ('dt'). not ($ ('dd'). prev ('dt')). remove();, .

+3

- ...

$('dt').each(function() {
    if($(this).next('dd').length == 0){
       $(this.remove();
    }
});
+2

:

$('dl>dt').each(function() {
    if ($(this).next("dd").length <= 0){
        $(this).remove();
    }
});
0

<dd>:

var $dl = $('dl'); // hook this up to your DL

$dl.find('dt').each(function() {
  var $dt = $(this), $next = $dt.nextAll(':visible').slice(0,1);
  // the next visible element is a dt OR there isn't another visible element.
  if ($next.slice(0,1).is('dt') || $next.length == 0)
  {
    $dt.hide();
  } else {
    $dt.show();
  }
});      
0

Source: https://habr.com/ru/post/1726528/


All Articles