0

   

 var addtocartbuttons = document.getElementsByClassName(`cart`)
    for (let i = 0; i < addtocartbuttons.length; i++) {
      var button = addtocartbuttons[i]
      button.addEventListener(`click`, lang1)
    }

    function lang1(event) {
      var target = event.srcElement;
      var button = event.target
      var shopitem = button.parentElement.parentElement.parentElement
      var title = shopitem.getElementsByClassName(`title`)[0].innerText;
      console.log(title)
      res2 = document.getElementsByClassName(`result2`)
      res2.textContent = title
      console.log(res2)
    }
  

  <div class="product-categorie-box">
    <div class="tab-content">
      <div role="tabpanel" class="tab-pane fade show active" id="grid-view">
        <div class="row">
          <div class="col-sm-6 col-md-6 col-lg-4 col-xl-4">
            <div class="products-single fix">
              <div class="box-img-hover">
                
                <img src="../../petuta/pizza/margerita.jpg" class="img-fluid" alt="Image">
                <div class="mask-icon">
                  <ul>
                    <li><a href="#" data-toggle="tooltip" data-placement="right" title="View"><i class="fas fa-eye"></i></a></li>
                    <li><a href="#" data-toggle="tooltip" data-placement="right" title="Compare"><i class="fas fa-sync-alt"></i></a></li>
                    <li><a href="#" data-toggle="tooltip" data-placement="right" title="Add to Wishlist"><i class="far fa-heart"></i></a></li>
                  </ul>
                  <a class="cart" href="#">Add to Cart</a> </div>
              </div>
              <div class="why-text">
                <h4 class="title" id="title1"> Kids</h4>
                <h5 class = "price"> €6.00</h5>
              </div>
            </div>
          </div>

but when button is pressed the text content in res2 despairs and put the new data any ideas?

Here is the console.log:

(HTMLCollection [div.result2, textContent: "Olives"]
0: div.result2
length: 1
textContent: "Olives")

The array only stays of the length of 1 I want it to keep storing how can I do that? I want to store the text content in res2 even when the button is clicked. I'm making a website which sends email with an order, and when I send email only the first one get sends and even in the console it appears the length of 1 then gets reset when add to cart button is clicked. ps. i have 33 of these product-categorie-box so I would need some fix I didnt use get element by id i used get element by class yet i still get one only can

37bandiko
  • 9
  • 4
  • 1
    Can you verify the code you have in your question? You have 2 functions both named `lang1`, and you seem to be missing a closing brace after your first function. Also, please edit your question to add any relevant HTML. Making this a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) will help us help you. – WOUNDEDStevenJones Mar 25 '21 at 18:25
  • yes its a typo ty – 37bandiko Mar 25 '21 at 18:27
  • Hi @37bandiko , please provide the relevant HTML and CSS files to see if they are consistent with the JS code you have provided. This will help us in debugging your code and provide better answers. Cheers! – rishichawda Mar 25 '21 at 18:31
  • didnt provide the css because i dont think it will effect it – 37bandiko Mar 25 '21 at 18:41
  • 1
    please provide working snippet. Where have you used `title` and `result2` – Dhara Mar 25 '21 at 18:57
  • `shopitem.getElementsByClassName(`title`)[0].innerText` you don't have any HTML elements with `class="title"`. I think you're just looking for `res2.textContent += ", " + title` to append the title of this clicked element to `res2`, but you probably also want to create an [array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) variable to keep track of these items from in your cart, rather than relying on text output to keep track of items. – WOUNDEDStevenJones Mar 25 '21 at 19:06
  • PS: use also `textContent` instead of `innerText` – Roko C. Buljan Mar 25 '21 at 19:07
  • @WOUNDEDStevenJones i did update the code i posted a different code by mistake – 37bandiko Mar 25 '21 at 19:11
  • Tip for future questions - make sure your code is present in the question (and formatted well!) and updated to be correct. New questions get a lot more views than questions a few hours old, so if your question needs a lot of back-and-forth in order to make it understandable, it's unlikely it will get answered effectively. – WOUNDEDStevenJones Mar 25 '21 at 19:58

0 Answers0