How to remove an element in HTML?

Asked

Viewed 151 times

-1

I need horizontally remove one DIV. The problem that is occurring is that clicking remove is being removed the entire column of DIV.

HTML

<div class="engloba-tudo">

    <div class="produtos container-cart"></div>

    <div id="a" class="subtotal">$</div>

</div>

<div class="item flex">
    <div class="caixinha" onclick="comprar(1)" id="1">
        CARRINHO
    </div>
</div>

CSS

.engloba-tudo {
    position: fixed;
    bottom: 0;
    right: 0;
    width: 30%;
    border-left: 1px solid #333;
    border-right: 1px solid #333;
    border-bottom: 1px solid #333;
    background: #eee;
    z-index: 9999;
}

.subtotal {
    width: 100%;
    border-top: 1px solid #333;
    padding: .7rem;
    cursor: pointer;
    box-sizing: border-box;
    flex-direction: column;
    text-align: center;
}

.img-info, .preco-info, .x-info {
    margin: 5px;
    padding: 4px;
    border: 1px solid grey;
    width: 25%;
    cursor: pointer;
}

.container-cart {
    max-width: 400px;
    margin: 0 auto;
    display: flex;
    border: 1px solid red;
    flex-direction: column;

}

.linha {
    width: 100%;
    flex-wrap: wrap;
    flex-direction: row;
    display: flex;
}

.item-cart {

    flex: 1;
    margin: 5px;
    text-align: center;
    font-size: 1.5em;
    width: calc(33% - 5px);

}

.fas {
    font-size: 50px;
}

JQUERY

var objetos = Array();

$(".subtotal").click(function () {
    $(".produtos").slideToggle(300);
})

function comprar(c) {
    if (c === 1) {
        var preco = '5,00';

        objetos.push(preco);

        var total = objetos.reduce(function (anterior, atual) {
            return parseFloat(anterior) + parseFloat(atual);
        });

        document.getElementById("a").innerHTML = '$ ' + total;

    }
}

$('#1').click(function () {
    var html = '<div class="linha">'
        + '<div class="item-cart img-info">img</div>'
        + '<div class="item-cart preco-info">preço</div>'
        + '<div class="item-cart x-info" onclick="remover(this)">x</div>'
        + '</div>';

    $('.produtos').append(html);
});

function remover(elemento) {
    elemento.parentNode.remove();

    objetos.splice(0,1);

    var total = objetos.length ? objetos.reduce(function(anterior, atual) {
          return parseFloat(anterior) + parseFloat(atual);
      }) : '';

      document.getElementById("a").innerHTML = '$ ' + total;
}
  • Could you format the code? for better understanding

  • 1

    sorry, error, link in codepen https://codepen.io/anderson-garden/pen/EzVMxj

  • @Andersongarden I suggest to you that before creating a publication, read this link carefully.

1 answer

0


You are adding the elements inside the div that you remove at the end, I made some changes to the code so that whenever it adds it creates a line. When removing, it will remove only this line, follows code:

var objetos = Array();

$(".subtotal").click(function () {
    $(".produtos").slideToggle(300);
})

function comprar(c) {
    if (c === 1) {
        var preco = '5,00';
        
        objetos.push(preco);

        var total = objetos.reduce(function (anterior, atual) {
            return parseFloat(anterior) + parseFloat(atual);
        });

        document.getElementById("a").innerHTML = '$ ' + total;

    }
}

$('#1').click(function () {
    var html = '<div class="linha">'
        + '<div class="item-cart img-info">img</div>'
        + '<div class="item-cart preco-info">preço</div>'
        + '<div class="item-cart x-info" onclick="remover(this)">x</div>'
        + '</div>';

    $('.produtos').append(html);
});

function remover(elemento) {
    elemento.parentNode.remove();
    
    objetos.splice(0,1);
  
    var total = objetos.length ? objetos.reduce(function(anterior, atual) {
	  	  return parseFloat(anterior) + parseFloat(atual);
	  }) : '';

	  document.getElementById("a").innerHTML = '$ ' + total;
}
.engloba-tudo {
    position: fixed;
    bottom: 0;
    right: 0;
    width: 30%;
    border-left: 1px solid #333;
    border-right: 1px solid #333;
    border-bottom: 1px solid #333;
    background: #eee;
    z-index: 9999;
}

.subtotal {
    width: 100%;
    border-top: 1px solid #333;
    padding: .7rem;
    cursor: pointer;
    box-sizing: border-box;
    flex-direction: column;
    text-align: center;
}

.img-info, .preco-info, .x-info {
    margin: 5px;
    padding: 4px;
    border: 1px solid grey;
    width: 25%;
    cursor: pointer;
}

.container-cart {
    max-width: 400px;
    margin: 0 auto;
    display: flex;
    border: 1px solid red;
    flex-direction: column;

}

.linha {
    width: 100%;
    flex-wrap: wrap;
    flex-direction: row;
    display: flex;
}

.item-cart {

    flex: 1;
    margin: 5px;
    text-align: center;
    font-size: 1.5em;
    width: calc(33% - 5px);

}

.fas {
    font-size: 50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="engloba-tudo">

    <div class="produtos container-cart"></div>

    <div id="a" class="subtotal">$</div>

</div>

<div class="item flex">
    <div class="caixinha" onclick="comprar(1)" id="1">
        CARRINHO
    </div>
</div>

  • 1

    Thank you so much is exactly what I wanted, I’ve been racking my brain for almost 2 days trying to solve this, thank you so much

  • @Andersongarden puts it in the title of the question that has been resolved, and if possible, says that the answer is right, so that other people can use it as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.