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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                 <!-- Здесь выбор объектов-->
        <div class="product-block">
          <h5>Смартфон Samsung Galaxy A02s 32GB White</h5>
        </div>
        <div class="product-block">
          <h5>Смартфон Samsung Galaxy A02s 32GB White</h5>
        </div>
        <div class="product-block">
          <h5>Смартфон Samsung Galaxy A02s 32GB White</h5>
        </div>
        <div class="product-block">
          <h5>Смартфон Samsung Galaxy A02s 32GB White</h5>
        </div>
        <div class="product-block">
          <h5>Смартфон Samsung Galaxy A02s 32GB White</h5>
        </div>
        <div class="product-block">
          <h5>Смартфон Samsung Galaxy A02s 32GB White</h5>
        </div>
<!-- Сюда добавляются заголовки-->
<hr>
<div class="favourite_elements">
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                console.clear();
let products = document.querySelectorAll(".product-block");
let product_names = document.querySelectorAll(".product-block h5");
let favourite = document.querySelector(".favourite_elements");
let favourite_names = document.querySelectorAll(".favourite_elements h5");
let h5_storage = new Array(products.length);
let h5 = document.createElement("h5");
// Хранилище для ссылок на копируемые элементы H5
let fav_arr = new Array();
let claimed = new Array();
let id;
const compare = localStorage.getItem("claimed");
if (compare != null) {
  for (var k = 0; k < compare.length; k++) {
    if (Number(compare[k]) > 0) {
      id = Number(compare[k]) - 1;
      products[id].style.background = "orange";
      favourite.insertAdjacentElement(
        "afterbegin",
        product_names[id].cloneNode(true)
      );
      fav_arr.push(product_names[id].outerHTML);
      claimed.push(compare[k]);
    }
  }
}
console.log(fav_arr);
products.forEach((product, index) => {
  product.addEventListener("click", function () {
    toggle_favourite(this, index); // this - ссылка на кликнутый product
  });
});

function toggle_favourite(product, index) {
  console.log(product, index);
  
  // зачем => let i = [].indexOf.call(products, product);
  let i = index
  
  // Номер кликнутого product среди всех остальных продуктов
  if (product.style.background != "orange") {
    h5_storage[i] = product_names[i].cloneNode(true);
    // h5 клонируется, ссылка на клон сохраняется в хранилище под тем же номером.
    product.style.background = "orange";
    fav_arr.push(h5_storage[i].innerHTML);
    localStorage.setItem("h5", JSON.stringify(fav_arr));
    claimed.push(i + 1);
    localStorage.setItem("claimed", claimed);
    favourite.insertAdjacentElement("afterbegin", h5_storage[i]);
    console.log(fav_arr);
  } else {
    product.style.background = "white";
    for (var j = 0; j < fav_arr.length; j++) {
      if (fav_arr[j] == product_names[i].innerHTML) {
        delete fav_arr[j];
      }
    }
    fav_arr = fav_arr.filter(function (x) {
      return x !== undefined && x !== null;
    });
    claimed = claimed.filter(function (x) {
      return x !== undefined && x !== null && x !== " ";
    });
    for (var s = 0; s < claimed.length; s++) {
      if (claimed[s] == i + 1) {
        delete claimed[s];
        delete fav_arr[i];
      }
    }
    console.log("remove child index", i, " of array ", h5_storage);
    favourite.removeChild(h5_storage[i]);
    localStorage.removeItem("h5");
    localStorage.setItem("h5", fav_arr);
    localStorage.removeItem("claimed");
    localStorage.setItem("claimed", claimed);
    console.log(claimed);
  }
}
for (var y = 0; y < favourite_names.length; y++) {
  if (favourite_names[y].innerHTML == "undefined") {
    favourite_names.parentNode.removeChild(favourite_names);
  }
}

              
            
!
999px

Console