Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a few elements on the page
<div class="product" data-id="1">
<div class="product_pic" style="background: url('static/img/product-2.jpg') no-repeat; background-size: auto 100%; background-position: center"></div>
<span class="product_name">Воздушные шары</span>
<span class="product_price">100 руб.</span>
<button class="js_buy">Buy</button>
</div>
how click on button.js_buy I can get value from div.data-id and send it in form order opened to click this?

What I have tried:

let btnGoods = document.querySelectorAll('.js_buy');
for (var i = 0; i < btnGoods.length; i++) {
btnGoods[i].addEventListener('click', () => {
console.log(this.parentNode.getAttribute('data-id'));
});
}
I get undefined value in console...
Posted
Updated 29-Oct-19 6:28am

Quote:
JavaScript
let btnGoods = document.querySelectorAll('.js_buy');
for (var i = 0; i < btnGoods.length; i++) {
    btnGoods[i].addEventListener('click', () => {
        console.log(this.parentNode.getAttribute('data-id'));
    });
}
I get undefined value in console.

The problem is that you're using an arrow function[^] for the event listener. Arrow functions don't get their own this reference; they inherit it from the outer scope.

If you change your listener code to console.log(this);, you'll see that this is the global Window object.

You can either change your event listener to be a regular function:
JavaScript
let btnGoods = document.querySelectorAll('.js_buy');
for (var i = 0; i < btnGoods.length; i++) {
    btnGoods[i].addEventListener('click', function() {
        console.log(this.parentNode.getAttribute('data-id'));
    });
}
Or use the event object to find the target:
JavaScript
let btnGoods = document.querySelectorAll('.js_buy');
for (var i = 0; i < btnGoods.length; i++) {
    btnGoods[i].addEventListener('click', e => {
        var clickedButton = e.target || e.srcElement;
        console.log(clickedButton.parentNode.getAttribute('data-id'));
    });
}
Event.target - Web APIs | MDN[^]
 
Share this answer
 
You can try accessing it like this:

JavaScript
function myFunction() {
  var div = document.getElementsByTagName("div")[0].getAttribute("product"); 
  var id = div.getAttribute('data-id'); 
}


When using the approach above, you must need to know the index value of the div hierarchy in the page, otherwise you will get an unexpected behavior. So I personally won't recommend it.


If you want to use JavaScript, then you may need to set an id/name attribute for your div elements so you can easily reference them.

HTML
<div id="divProduct" class="product" data-id="1"> </div>


You can then get the value of data-id attribute like this:

JavaScript
<script>
function myFunction() {
  //reference the div
  var plant = document.getElementById('divProduct');
  //get the value of data-id attribute
  var id = plant.getAttribute('data-id'); //return 1
  
}
</script>


You can then call the myFunction() method by attaching an onclick event to your Button:

HTML
<button class="js_buy" onclick="myFunction();">Buy</button>



If you can use jQuery, then you can simply access DOM elements using selectors without the need to specify an id or name attribute or modify your HTML. For example:

JavaScript
<script>
$(".js_buy" ).click(function() {
  var id = $(".product").attr("data-id"); // will return the value 1
});
</script>
 
Share this answer
 
Comments
Richard Deeming 29-Oct-19 12:28pm    
All of those solutions assume there's only one product <div> on the page. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900