I am trying to map a data-id attribute value to a Javascript Array name.
I would like to have product = productid01 where productid01 is the text value of data-id.
<html>
<table>
<tr>
<td>$200<button type="button" class="function-buy" data-id="productid01" id="btn-productid01">Buy</button></td>
</tr>
</table>
</html>
<script>
$( document ).ready(function() {
var productid01 = {desc:"This is a product", price:200.00};
$(document).on('click', '.function-buy', function(){
id = $(this).data("id"); //id = productid01
var product = id; //var product = productid01 (would like !)
document.write(product.desc); //Expected output : This is a product
});
});
</script>
I have discovered that eval() seems to work :
var product = eval(id);
but is there a better way to achieve this without using eval() ?
For sure i have a kind of datatype mismatch here, but i cannot figure out how to work around this.
Thanks for your help.
eval?data-idattribute? Is your product list being stored as an array in memory somewhere?