I want to access the raw output of a HTML element that contains Javascript in it.
The HTML element (it's running inside of a PHP loop that cycles products currently in the cart):
<h5 id="price-<?php echo $i ?>">US$ <script>var price = <?php echo $product['price'] ?>; document.write(price);</script></h5>
Later in the code I've a function that runs when the quantity is changed.
The Javascript function reads & prints the quantity correctly, but it doesn't read the price correctly (returns a "undefined" when pricet is to be outputted).
function updatetotal(i){
var qty = document.getElementById(i).value;
var pricet = document.getElementById('price-'+i).value;
document.getElementById('sub-'+i).innerHTML = '$ '+pricet; //Testing purposes
}
Here is the line of code where output appears (The script is just to load the initial item total on page load) (Note this is inside of the PHP loop that cycles products currently in the cart, i.e. PHP variable i is the item's number in the cart):-
<h5 id="sub-<?php echo $i ?>">US$ <script>var qty = document.getElementById('<?php echo $i ?>').value; var sub_price = price * qty; total_price += sub_price; document.write(sub_price)</script></h5>
The function is called inside the on-click event of these two buttons that increase/decrease the quantity of an item:-
<input type="text" name="<?php echo $i ?>" id="<?php echo $i ?>" maxlength="12" value="<?php echo $qty[$i] ?>" title="Quantity" class="input-text qty"/>
<button onclick="var result = document.getElementById('<?php echo $i ?>'); var sst = result.value; if( !isNaN( sst )) result.value++;updatetotal(<?php echo $i ?>);return false;" class="increase items-count" type="button"><i class="lnr lnr-chevron-up"></i></button>
<button onclick="var result = document.getElementById('<?php echo $i ?>'); var sst = result.value; if( !isNaN( sst ) && sst > 0 ) result.value--;updatetotal(<?php echo $i ?>);return false;" class="reduced items-count" type="button"><i class="lnr lnr-chevron-down"></i></button>
(I have omitted extra code for the sake of keeping the code presented short & to the point. It's available if you want more information)