Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div class="row">
  <!-- el producto ----->
 <article class="producto card col-5 m-4" id="P12">
    <a href="http://"><img src="img/miProducto.jpg"></a>
    <h3><a href="http://">Zapatos</a></h3>
    <p class="precio">325.00</p>
    <i class="btn_carrito btn btn-success">mi boton carrito</i>
 
</article>

 <article class="producto card col-5 m-4" id="P13">
    <a href="http://"><img src="img/miProducto.jpg"></a>
    <h3><a href="http://">Polos</a></h3>
    <p class="precio">325.00</p>
    <i class="btn_carrito btn btn-success">mi boton carrito</i>
 
</article>


 <article class="producto card col-5 m-4" id="P14">
    <a href="http://"><img src="img/miProducto.jpg"></a>
    <h3><a href="http://">Pantalon</a></h3>
    <p class="precio">325.00</p>
    <i class="btn_carrito btn btn-success">mi boton carrito</i>
 
</article>

 <article class="producto card col-5 m-4" id="P15">
    <a href="http://"><img src="img/miProducto.jpg"></a>
    <h3><a href="http://">Gorra</a></h3>
    <p class="precio">325.00</p>
    <i class="btn_carrito btn btn-success">mi boton carrito</i>
   
</article>

  
</div>


<br>

<!-- la lista ----------->
<form class="lista_carrito card col-5 mx-auto">
    <div class="text-right">
        <span class="">Lista de productos seleccionados</span>
    </div>
    <ul class="lista_de_productos list-unstyled border mx-2">
      <!-- aqui agrego las listas con javascript -->
     </ul>
</form>
              
            
!

CSS

              
                
              
            
!

JS

              
                let lista_de_productos = document.querySelector(".lista_de_productos");
let carrito_btn = document.querySelectorAll(".btn_carrito");

let productos_seleccionados = []
let arrayProductos =[]
let cantidad_c = 1

let addProducto = (event) => {
  
  lista_de_productos.innerHTML =''
  idProducto = event.currentTarget.parentElement.id

      /*Verifica si el id ya se encuentra en la lista, si no es así lo agrega al array ids_productos_seleccionados*/
  
      if(arrayProductos.indexOf(idProducto)<0)
        {
            arrayProductos.push(idProducto)
            productos_seleccionados.push({
                                                producto:idProducto, 
                                                cantidad: cantidad_c
                                            })
        }
     else
        {
            for(let product of productos_seleccionados)
              {
                  if(product['producto'] == idProducto)
                    {
                        product['cantidad'] += 1
                    }
              }
            
         }


  for(let element of productos_seleccionados){
    
          let nombre_c = document.querySelector(`#${element['producto']} h3 a`).innerHTML   
          let precio_c = parseFloat(document.querySelector(`#${element['producto']} .precio`).innerHTML).toFixed(2)

          lista_de_productos.innerHTML += `<li id='carrito_${element}'>
                                <div class='nombre_p_list w-50'>${nombre_c}</div>
                                <input type='text' name='' value='${element['cantidad']}'>
                                <span class='mx-3 cerrar_lista'>x</span>
                                <span class='list_price'>${precio_c}</span>
                                </li>`
        
        
   
    
      }



}

carrito_btn.forEach(boton => {
    boton.addEventListener("click", addProducto);
})




              
            
!
999px

Console