Solution 1 :

Take a good look at what dataset vs .data – Difference? dataset do, or data for that matter, and then take a look at your inner HTML in .productItem there is not a single data attribute inside, no wonder dataset can not fetch any and is undefined. Please tel me what is wanted result in console.log(productInfo[0].name); What is expected to be inside and we will extract it.

You can fetch a name by simply doing:

$(this).text()

And to clarify, your click function is extracting data from HTML, not JSON data itself. You put that JSON data into dynamically reddened HTML, and you get it with on click.

Also if only name is wanted result, keep in mind if you add any more text into .productItem you will have dig a bit deeper with click selector, adjust it. In current example is working.
Example:

//$.getJSON("assets/products/sample_products.json", function(data) {
  var product_data = '';
  var data=  { "data" : [{"id":"1333","article_number":"4016","barcode":"heeftgeenbarcode4","name":"White Albino"}] };
  
  $.each(data.data, function(key, value) {
    // console.log(data);
    product_data += '<div class="col-3 productCard">';
    product_data += '<a href="#" class="productItem">';
    product_data += '<div class="card">';
    product_data += '<img src="assets/images/Firecracker.jpg" alt="Avatar" style="width:100%; height: 8vh;">';
    product_data += '<div class="container">';
    product_data += '<p>' + value.name + '</p>';
    product_data += '</div>';
    product_data += '</div>';
    product_data += '</a>';
    product_data += '</div>';
  });
  $('#touchViewProducts').append(product_data);


  //function to add item to shopping cart
  $(".productItem").click(function(e) {
    e.preventDefault();
    
    console.log($(this).text());
    var productInfo = $(this.dataset);
    //console.log(productInfo[0].name);

  })
//})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row touchViewSection">
  <!-- shopping sector -->
  <!-- touchView -->
  <!-- categories menu -->
  <div class="col-3 categoriesSection">
    <div class="categories">
      <p style="background-color: white; margin-bottom: 0px"> Categories </p>
      <a class="nav-link" id="all" href="#">All</a>
      <a class="nav-link" id="black-thunder" href="#">Black-thunder</a>
      <a class="nav-link" id="blue-eagle-fireworks" href="#">Blue-eagle-fireworks</a>
      <a class="nav-link" id="crystal-exclusive" href="#">Crystal-exclusive</a>
      <a class="nav-link" id="empire-fireworks" href="#">Empire-fireworks</a>
      <a class="nav-link" id="grondbloemen" href="#">Grondbloemen</a>
    </div>
  </div>

  <!-- categories menu -->
  <!--                <p style="background-color: white; margin-bottom: 0px" > Products </p>-->
  <div class="col-9 productItems">
    <br>
    <div class="row" id="touchViewProducts">

    </div>
  </div>
</div>
<!--/touchView -->
<!--Keyboard View -->
<div class="row keyboardViewSection">
  <div class="col-12 keyboardViewRow">
    <table id="data-table" class="table table-bordered" style="width: 100%;">
      <thead id="tableHead">
        <tr>
          <th> # </th>
          <th> Product name </th>
          <th> Free Stock </th>
          <th> Price </th>
          <th> Action </th>
        </tr>
      </thead>
    </table>
  </div>
</div>
<!--/Keyboard View -->
<div class="footer">
  <div class="container">
    <p class="text-muted"> Developed by Vesta Group</p>
  </div>
</div>
</div>
<!--/shopping sector-->
<div class="col-4 cartSection">
  <!--cart-->
  <div class="row">
    <div class="col-5">Product</div>
    <div class="col-1">Pcs.</div>
    <div class="col-2">Price</div>
    <div class="col-3">Total</div>
  </div>
  <hr style="background-color: white;">

  <div class="row cartCheck">
    <div class="col-5">Number of products</div>
    <div class="col-1">1</div>
    <div class="col-2">Subtotal</div>
    <div class="col-3 total">&euro; 0,00</div>

    <div class="col-5"></div>
    <div class="col-1"></div>
    <div class="col-2">Total </div>
    <div class="col-3">&euro; 0,00</div>

    <div class="col-12 checkoutBtn"> Checkout </div>
    <div class="col-6 addDiscountBtn"> Add discount </div>
    <div class="col-6 cancelBtn"> Cancel </div>
  </div>
  <!--/cart-->
</div>
</div>
</div>

</div>

Problem :

I’m trying to make my functionality work that eventually has to add items to my shopping cart. However, the tutorial I’m following is making use of data in his html that he passed down with a data type in the element itself. I’m retreiving data from a json file. I would like to know how I can make this simple functionality work, so I can continue on working out the functionality. At this moment I’m getting back “undefined” in my console.

Html code:

           <div class="row touchViewSection">
                <!-- shopping sector -->
                <!-- touchView -->
                <!-- categories menu -->
                <div class="col-3 categoriesSection">
                    <div class="categories">
                        <p style="background-color: white; margin-bottom: 0px" > Categories </p>
                        <a class="nav-link" id="all" href="#">All</a>
                        <a class="nav-link" id="black-thunder" href="#">Black-thunder</a>
                        <a class="nav-link" id="blue-eagle-fireworks" href="#">Blue-eagle-fireworks</a>
                        <a class="nav-link" id="crystal-exclusive" href="#">Crystal-exclusive</a>
                        <a class="nav-link" id="empire-fireworks" href="#">Empire-fireworks</a>
                        <a class="nav-link" id="grondbloemen" href="#">Grondbloemen</a>
                    </div>
                </div>

                <!-- categories menu -->
<!--                <p style="background-color: white; margin-bottom: 0px" > Products </p>-->
                <div class="col-9 productItems" >
                    <br>
                    <div class="row" id="touchViewProducts">

                    </div>
                </div>
            </div>
            <!--/touchView -->
            <!--Keyboard View -->
            <div class="row keyboardViewSection">
                <div class="col-12 keyboardViewRow">
                    <table id="data-table" class="table table-bordered" style="width: 100%;">
                        <thead id="tableHead">
                        <tr>
                            <th> # </th>
                            <th> Product name </th>
                            <th> Free Stock </th>
                            <th> Price </th>
                            <th> Action </th>
                        </tr>
                        </thead>
                    </table>
                </div>
            </div>
            <!--/Keyboard View -->
            <div class="footer">
                <div class="container">
                    <p class="text-muted"> Developed by Vesta Group</p>
                </div>
            </div>
        </div>
        <!--/shopping sector-->
        <div class="col-4 cartSection">
            <!--cart-->
            <div class="row">
                <div class="col-5">Product</div>
                <div class="col-1">Pcs.</div>
                <div class="col-2">Price</div>
                <div class="col-3">Total</div>
            </div>
            <hr style="background-color: white;">

            <div class="row cartCheck">
                <div class="col-5">Number of products</div>
                <div class="col-1">1</div>
                <div class="col-2">Subtotal</div>
                <div class="col-3 total">&euro; 0,00</div>

                <div class="col-5"></div>
                <div class="col-1"></div>
                <div class="col-2">Total </div>
                <div class="col-3">&euro; 0,00</div>

                <div class="col-12 checkoutBtn"> Checkout </div>
                <div class="col-6 addDiscountBtn"> Add discount </div>
                <div class="col-6 cancelBtn"> Cancel </div>
            </div>
            <!--/cart-->
        </div>
    </div>
</div>

</div>
    

JS code:

   <script>
        $.getJSON("assets/products/sample_products.json", function(data) {
            var product_data = '';
            $.each(data.data, function (key, value) {
                // console.log(data);
                product_data += '<div class="col-3 productCard">';
                product_data += '<a href="#" class="productItem">';
                product_data += '<div class="card">';
                product_data += '<img src="assets/images/Firecracker.jpg" alt="Avatar" style="width:100%; height: 8vh;">';
                product_data += '<div class="container">';
                product_data += '<p>' + value.name + '</p>';
                product_data += '</div>';
                product_data += '</div>';
                product_data += '</a>';
                product_data += '</div>';
            });
            $('#touchViewProducts').append(product_data);


            //function to add item to shopping cart
            $(".productItem").click(function(e){
                e.preventDefault();

                var productInfo = $(this.dataset);
                console.log(productInfo[0].name);

            })
        })
  
    </script>

enter image description here

Comments

Comment posted by ikiK

You are getting undefined where?!? On what line of code, what is undefined? Also add sample of data of what you console logging in

Comment posted by Yoverflow

@ikiK I’m getting an error in the console.log, when the click function gets triggered. This is quick example of an object in my json file: { “data” : [{“id”:”1333″,”article_number”:”4016″,”barcode”:”heeftgeenbarcode4″,”name”:”White Albino”}]

Comment posted by Yoverflow

@ikiK thanks for your reaction, I’m kinda new to this. I want to show the name of the first product in the array of my json data, which in this case would be “White Albino”.

By