0

Convert Ounces To Pounds Easily With jQuery

This is a script I wrote for allowing me to convert product weights from ounces to pounds since the Phpurchase WordPress plugin uses pounds for the Live shipping calculations for shipping costs per product based on weight in pounds.

Simply copy and paste a number in the right input, and it divides it by 16 and spits the result into the left input once you remove focus on the right input field. Very intuitive and convenient for ounce to pound conversion.

Here’s a live working example of this:
http://www.jsfiddle.net/jaredwilli/QVqsa/


<script>
$(function() {
    $('#product_weight').parent()
        .find('.label_desc').after('<small>&laquo; oz</small> <input type="text" id="oz" value="" size="1" /><br /><br />');

    function roundVal(val){
        var dec = 2,
            lb = Math.round(val*Math.pow(10, dec))/Math.pow(10, dec);
        return lb;
    }

    $('#oz').change('keyup', function() {
        var oz = $(this),
            lb = parseFloat( $(oz).val() / 16);
            $(oz).attr('value','');
        $('input#product_weight')
            .attr('value', roundVal(lb) );
    });
});
</script>

An example of the HTML code to use:

<ul type="none">
<li>
    <label class="med" for="product[weight]">Weight:</label>
    <input name="product[weight]" value="" size="6" id="product_weight" type="text"> lbs
</li>
</ul>
Levels: Syntax: ,

  1. Avatar

    Your Name
    May 17


    CommentLuv badge