JSFiddle - React, Tailwind, and code Playground

by jessekinsman

HTML

<h1> Image Size Calculator </h1>
<h2> Convert Pixels to Inches or Inches to Pixels</h2>
<form class="pixel-convert">
<p>
<input type="text" name="width" id="width" /><label for="width">Width</label></p>
<p>
<input type="text" name="height" id="height" /><label for="height">Height</label><p>
<p>
<select id="type">
<option value="pixels">Select a convert option</option>
<option value="pixels">Convert inches to pixels</option>
<option value="inches"> Convert pixels to inches</option>
</select>
<input type="button" id="clear" value="Clear Results" />
</p>
</form>

CSS

body {
        font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
    font-size: 16px;
}
h1 {
    font-size: 1.8em;
    color: #A1A1A1;
    font-weight: 500;
    margin-bottom: 5px;
}
h2 {
    font-size: 1.2em;
    color: #CBCBCB;
    margin-bottom: 5px;
}
.result {
    margin: 10px;
    background: #E1E1E1;
    padding: 15px;
    border-radius: 4px;
}
.result h3 {
   font-size: 1.5em;
    font-weight: 600;
    color: #C0C0C0;
    font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
}
.result p {
     color: #818181;
    font-size: 1em;
    font-weight: 400;
    font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
}
.result strong {
    font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
    font-weight: bolder;
    font-size: 1.2em;
    color: #000;
}
.note {
    font-size: .8em;
}
.pixel-convert label {
    font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
    font-size: 1em;
    color: #818181;
    font-weight: 600;
}
.pixel-convert input[type="text"] {
    font-size: 1.5em;
    border-radius: 5px;
    border: 1px #818181 solid;
    padding: 10px;
    font-weight: 600;
    margin: 0 10px 10px 0;
}
.pixel-convert p {
    padding: 5px;
}

JavaScript

jQuery(document).ready(function() {
    var printSize;
    var webPress;
    var screenSize;
    var pixelSize;
    var pixelSizeMed;
    var pixelSizeLow;
    jQuery("#type").change(function() {
        var imgWidth = jQuery("#width").val();
        var imgHeight = jQuery("#height").val();
        var cType = jQuery("#type").val();
        console.log("Type: " + cType + " Width: " + imgWidth + " Height: " + imgHeight);
        if (cType.length > 0 && cType == "pixels") {
            console.log("pixels");
            pixelSize = inchToPixel (imgHeight, imgWidth, "300");
            formatAndAppend(pixelSize, "pixel",imgHeight,imgWidth,"pixels","300");
            pixelSizeMed = inchToPixel (imgHeight, imgWidth, "150");
            formatAndAppend(pixelSizeMed, "pixMed",imgHeight,imgWidth,"pixels","150");
            pixelSizeLow = inchToPixel (imgHeight, imgWidth, "72");
            formatAndAppend(pixelSizeLow, "pixLow",imgHeight,imgWidth,"pixels","72");
        }
        if (cType.length > 0 && cType == "inches") {
            printSize = pixelToInch (imgHeight, imgWidth, "300");
            formatAndAppend(printSize, "print",imgHeight,imgWidth,"inches","300");
            webPress = pixelToInch (imgHeight, imgWidth, "150");
            formatAndAppend(webPress, "webpress",imgHeight,imgWidth,"inches","150");
            screenSize = pixelToInch (imgHeight, imgWidth, "72");
            formatAndAppend(screenSize, "screen",imgHeight,imgWidth,"inches","72");
        }
    });
    
    jQuery("#clear").click(function(){
        jQuery(".result").remove();
    });
    
function inchToPixel (iHeight, iWidth, dpi) {
        if (iHeight.length > 0 && iWidth.length > 0 && dpi.length > 0 ) {
            console.log("function");
           iHeight = Number(iHeight);
           iWidth = Number(iWidth);
           dpi = Number(dpi);  
            if (dpi != "NaN" && iHeight != "NaN" && iWidth != "NaN")
            {                
           var tmpHeight = iHeight *...