You would need to pass the key to the item.
Something is mounting it on the screen right? Whether it’s a PHP or even an Angularjs, it doesn’t matter, when you build this screen, you need to bring a value that references the item.
The buy function will always be called, so within the function you need to pass a variable warning which will be the item, it can be the id of this item ( or even the name of the div, since each div needs to have its unique value).
For example:
<legend>Item 1:</legend>
<h1> Bola </h1>
<p> 5 reais </p>
<input type="button" value="Comprar" onclick="comprar('1')">
<legend>Item 2:</legend>
<h1> Chiclete </h1>
<p> 2 reais </p>
<input type="button" value="Comprar" onclick="comprar('2')">
function comprar(item){
//'item' corresponde ao valor passado na chamada da função, no exemplo, 1 ou 2
}
Note: There are better ways to do this through Jquery, for example.
An example: https://repl.it/@Dadinel/Affectionateharsharguments, good studies! :)
– Daniel Mendes