Skip to content Skip to sidebar Skip to footer

Strip Data To Number And Decimal Then Jquery Set Input Value To Variable

I'm taking data from an encrypted php script which outputs variables as e.g. %%Price%% Annoyingly those variables are not just a decimal and numeric value. The encrypted script ren

Solution 1:

I would use:

newPrice = parseFloat(price.substring(2, price.length - 3));

Solution 2:

I checked your code with a jsfiddle and it seems ok. Check here: http://jsfiddle.net/CmHn6/1/. I actually replaced:

var price = "%%Price%%";

with:

var price = '<fontcolor="green"><strong>Sale Price: $2,489.99</strong></font>';

as you said in your example. Notice that I used single quotes there. If your encrypted script does not take this into account, your line could end up like:

var price = "<fontcolor="green"><strong>Sale Price: $2,489.99</strong></font>";

which would result in a syntax error and would not fill in your input element. If your string never use single quotes, then you can use single quotes in your code, like in my example. If not, you could put %%Price%% inside a hidden div, and read this value from there:

<divid="pricediv"display="style: none">%%Price%%</div><formid="add-to-cart"><inputtype="hidden"name="price"id="price"value=""/><buttontype="submit">  Review & Order</button></form><script>var price = $("#pricediv").html();
var newPrice = price.replace(/[^0-9\.]/g, '');
jQuery("#price").val(newPrice);
</script>

Post a Comment for "Strip Data To Number And Decimal Then Jquery Set Input Value To Variable"