mardi 4 août 2015

onclick passing data to javascript

I have a link that when clicked should assign a rails variable to a JavaScript variable. The JavaScript is then used to calculate the price. I have the price calculated with a hard coded value at the moment.

in the html I need data-price to be var blankPrice in the js. Any help would be great.

function calculatePrice(){

                var blankPrice = 5;


                console.log(blankPrice)
                var pricePerSide = 3;

                var printedSides = 0;

                if (frontCanvas) {
                        var frontCanvasJson = JSON.parse(frontCanvas);
                        
                        if (frontCanvasJson && frontCanvasJson.objects.length > 0)
                                printedSides++;
                }

                if (backCanvas) {
                        var backCanvasJson = JSON.parse(backCanvas);

                        if (backCanvasJson && backCanvasJson.objects.length > 0)
                                printedSides++;
                }

                var total = blankPrice + (pricePerSide * printedSides);
                $('.base-price').text('$' + total);
         }

         function saveCampaign() {
                campaign.front_canvas = frontCanvas;

                $.post('/campaigns', campaign);
         }
<a 
   tabindex="-1" 
   data-original-title="<%= shirt_color.color_name.titleize %>"
   class="shirt-color-link" 
   data-color="#<%= shirt_color.hex %>" 
   data-price="<%= product.base_price %>" 
   data-product-id="<%= product.id %>">
   <table>
     <tr>
                <td style="border: 1px solid #DDD" bgcolor="#<%= shirt_color.hex %>"></td>
                <td style="padding-left: 10px;"><%= shirt_color.color_name.titleize %></td>
        </tr>
   </table>
</a>


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire