0

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.

2
  • This feels like you're over-thinking things or there's a behaviour you're not fully explaining. What else is it you're trying to retrieve on your click event that you're not already getting without the use of eval? Commented Jun 5, 2017 at 0:39
  • So you're trying to map the ID of a product to an actual object based on the value of the data-id attribute? Is your product list being stored as an array in memory somewhere? Commented Jun 5, 2017 at 0:44

2 Answers 2

1

You can make use of javascript objects, see example:

<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 products = {
    productid01 : {desc:"This is a product", price:200.00}
  };

 

  $(document).on('click', '.function-buy', function(){
   id = $(this).data("id");      //id = productid01
   var product = products[ id ]; // get the product01
   document.write(product.desc); //Expected output : This is a product
  });
 });
</script>

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, it was a pleasure to help.
1

You can define productidN as properties of an object, use bracket notation.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<table>
  <tr>
    <td>$200<button type="button" class="function-buy" data-id="productid01" id="btn-productid01">Buy</button></td>
  </tr>
</table>


<script>
  $(document).ready(function() {
  
    var options = {
      productid01: {
        desc: "This is a product",
        price: 200.00
      }
    };

    $(document).on('click', '.function-buy', function() {
      var id = $(this).data("id"); //id = productid01
      var product = id; //var product = productid01 (would like !)
      product = options[product];
      $("body").append(product.desc); //Expected output : This is a product
    });
  });
</script>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.