0

Why the following code does not run, It supposes to run according to http://jsfiddle.net/NF7x9/. On the full HTML page, it does not work. My second question is how to pass the Total Calculated Value to the next page where another asp script requires the total value.

<html>
<head>

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

<script type="text/javascript">
var subt_value = parseFloat($('#subt0').text());
var start_price = parseFloat($('#subt0').attr('data-original'));
        
        $("#ser1, #ser2").click(function(){
    
            var sub_total = 0
            var extra_fee = parseFloat($(this).attr('data-price')/1.12).toFixed(2);
                        
            $(this).each(function(){
                if($(this).is(":checked")){
                    var sub_total = subt_value + parseFloat(extra_fee);
                    $('#subt0').text(parseFloat(sub_total).toFixed(2));
                }else{
                    $('#subt0').text(parseFloat(start_price).toFixed(2));
                }
            });
           
            
       });       
       </script>
</head>

<body>
<form name="tst">
<label id="subt0" data-original="8.90">8.90</label>

<input name="service1" type="checkbox" id="ser1" data-price="1" value="1" title="Service 1" />
<input name="service2" type="checkbox" id="ser2" data-price="5" value="1" title="Service 2" />
</form>

</body>
</html>

Babar
  • 63
  • 1
  • 7
  • 2
    Move you script to the bottom of your document. or wrap it in $(document).ready(function() {}); – Carsten Løvbo Andersen Apr 23 '21 at 08:37
  • Problem is that you are trying to bind a click event to $("#ser1, #ser2") but they don't exist yet in the DOM. – Carsten Løvbo Andersen Apr 23 '21 at 08:39
  • This works in jsfiddle because jsfiddle always puts the [script] box just before the `

    ` tag - ie at the end of the body. Add `console.log($("#ser1, #ser2").length)` in your code - it will be `0` in the `

    `/at the start of `

    ` and `2` if you move your code to the end (inside `

    `)

    – freedomn-m Apr 23 '21 at 09:05
  • Does this answer your question? [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – freedomn-m Apr 23 '21 at 09:06
  • Thank you, it works now. Now second part of my question. – Babar Apr 23 '21 at 10:05
  • How to pass the Total Calculated Value to the next page where another asp script requires the total value. – Babar Apr 23 '21 at 10:05

0 Answers0