cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="calculate">
    <p><h1>Calculation arithmetic inline input text</h1>
    Inspired from cell box on office spreadsheet.</p>
    <hr/><br>
    <p>
    Try typing arithmetic e.g.(2+5/2-1*3) then hit enter or move out cursor<br>This will calculate 2+5/2-1*3 to be 1.5</p>
   
    Basic
    <input type="text" class="calc" placeholder="Insert arithmetic here"/>
    <br>
    With currency format
    <input type="text" class="calc" data="S" placeholder="1.123.231,02" style="font-size:17px;"/>
    <br>
    <input type="text" class="calc" data="N" placeholder="1,123,231.02" style="font-size:20px;"/>
    <br>
    With round decimal
    <input type="text" class="calc" data="N-2" placeholder="1,123,231.02"/>
    <br>
    <h2>How to use</h2><hr/><br>
    <p>Include inline-calc.js on your page before end of body tag</p>
    <pre><code>&lt;script src="path-to-inline-calc-js" type="text/javascript"&gt;&lt;/script&gt;</code></pre>
    <p>Then just add class calc and data attribut for currency format data="N" or data="S"</p>
    <pre><code>&lt;input type="text" class="calc" data="N"/&gt;</code></pre>
    <p>To using round decimal add format data="N-2" for 2 decimal place</p>
    <pre><code>&lt;input type="text" class="calc" data="N-2"/&gt;</code></pre>
    <br><br><button onclick="downloadCalcInline()" class="download">Download</button>
<pre><code id="inlineCalcJs">/*! Created By: Hangs Breaker */
document.getElementsByTagName("head")[0].insertAdjacentHTML('beforeend','&lt;style&gt;.calc{vertical-align:top;display:block;min-height:34px;padding: 4px 12px 2px 15px;font-size:14px;line-height: 1.42857;color: #555;background-color: #FFF;background-image: none;border: 1px solid #CCC;border-radius: 4px;box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.075) inset;transition: border-color 0.15s ease-in-out 0s, box-shadow 0.15s ease-in-out 0s;margin-left:8px;text-align:right;}.calcc{position:absolute;margin-left:8px;background:#EEEEEE;border:1px solid #ccc;border-radius:5px;min-width:9px;min-height:5px;display:block;padding:0px 2px 0px 2px;font-family:Arial;color:#555}&lt;/style&gt;');

var enKey = false;
var calc = document.querySelectorAll('.calc');
var forEach = [].forEach;
forEach.call(calc, function (el, i) {
    //calc[i].setAttribute("onblur", "calcs()");
    //calc[i].setAttribute("onclick", "getcalcs()");
    //calc[i].setAttribute("onkeypress", "handle(event)");
    calc[i].insertAdjacentHTML('beforeBegin','&lt;div class="calcc"&gt;c&lt;/div&gt;');
    calc[i].insertAdjacentHTML('afterEnd','&lt;input type="hidden" class="calcr" style="display:none;"/&gt;');
    var calcr = document.querySelectorAll('.calcr');
    
    function doCalc(){
        calcr[i].value = calc[i].value;
        if(calc[i].value != ''){
            if(calc[i].getAttribute("data")){
                var data = calc[i].getAttribute("data").split("-");
                if(data[0]==="N"){
                    calc[i].value = eval(calc[i].value.replace(/\,/g, ""))===undefined?'':eval(calc[i].value.replace(/\,/g, ""));
                    if(data[1] != null &amp;&amp; data[1] != ""){calc[i].value = parseFloat(calc[i].value).toFixed(data[1]);}
                    calc[i].value = calc[i].value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
                }else if(data[0]==="S"){
                    calc[i].value = eval(calc[i].value.replace(/\./g, "").replace(/\,/g, "."))===undefined?'':eval(calc[i].value.replace(/\./g, "").replace(/\,/g, "."));
                    if(data[1] != null &amp;&amp; data[1] != ""){calc[i].value = parseFloat(calc[i].value).toFixed(data[1]);}
                    calc[i].value = calc[i].value.toString().replace(/\./g, ",").replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");
                }
            }else{
                calc[i].value = eval(calc[i].value)===undefined?'':eval(calc[i].value);
            }
        }
    }
    function mouseDown(){
        if(calcr[i].value != ''){
            calc[i].value = calcr[i].value;enKey = false;
        }
    }
    function onBlur(){
        if(enKey){enKey = false;}else{doCalc();}
    }
    
    el.addEventListener('mousedown',mouseDown);
    el.addEventListener('blur',onBlur);
    el.addEventListener('keypress',function(e){
    var key = e.which || e.keyCode;
        if(key === 13){
            enKey = true;
            doCalc();
        }
    });
});
// Label for copy
var calcc = document.querySelectorAll('.calcc');
var forEach = [].forEach;
forEach.call(calcc, function (el, i) {
    function mouseEnter(){
        calcc[i].innerHTML = calc[i].value;
        calcc[i].style.padding = "6px 6px 6px 5px";
    }
    function mouseOut(){
        calcc[i].innerHTML = "c";
        calcc[i].style.padding = "0px 2px 0px 2px";
    }
el.addEventListener('mouseenter',mouseEnter);
el.addEventListener('mouseout',mouseOut);
});</code></pre>
<br><br>
<div class="copyright">Hangs Breaker &copy; 2015</div>
</div>
            
          
!
            
              *{margin:0;padding:0;}
body{
    background:#eee;
    font-family:Arial;
    color:#333;
}
p{padding:10px 0px;}
.calculate{
    position:relative;
    width:720px;
    margin:30px auto;
    padding:0px 30px 30px;
    border-radius:3px;
    border:1px solid #CCCCCC;
    background:#FFF;
}
.download{
    padding: .4em .9em;
    border-width: 1px 1px 2px;
    border-style: solid;
    border-radius: 0.16667em;
    cursor: pointer;
    background: #00aadc;
    border-color: #0090bb;
    border-top-color: #0099c6;
    border-bottom-color: #0088b0;
    color: #fff;
}
.download:hover, .download:focus {
    background: #33bbe3;
    border-color: #2b9ec0;
    border-bottom-color: #2895b5;
    color: #fff;
}
.copyright{
    text-align:center;
    font-size:12px;
}
hr{margin-top:5px;color:#fff;}
pre{
    word-wrap: normal;
    padding: 16px;
    overflow: auto;
    line-height: 1.45;
    background-color: #F7F7F7;
    border-radius: 3px;
}
            
          
!
            
              /*! Created By: Hangs Breaker */
document.getElementsByTagName("head")[0].insertAdjacentHTML('beforeend','<style>.calc{vertical-align:top;display:block;min-height:34px;padding: 4px 12px 2px 15px;font-size:14px;line-height: 1.42857;color: #555;background-color: #FFF;background-image: none;border: 1px solid #CCC;border-radius: 4px;box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.075) inset;transition: border-color 0.15s ease-in-out 0s, box-shadow 0.15s ease-in-out 0s;margin-left:8px;text-align:right;}.calcc{position:absolute;margin-left:8px;background:#EEEEEE;border:1px solid #ccc;border-radius:5px;min-width:9px;min-height:5px;display:block;padding:0px 2px 0px 2px;font-family:Arial;color:#555}</style>');

var enKey = false;
var calc = document.querySelectorAll('.calc');
var forEach = [].forEach;
forEach.call(calc, function (el, i) {
    //calc[i].setAttribute("onblur", "calcs()");
    //calc[i].setAttribute("onclick", "getcalcs()");
    //calc[i].setAttribute("onkeypress", "handle(event)");
    calc[i].insertAdjacentHTML('beforeBegin','<div class="calcc">c</div>');
    calc[i].insertAdjacentHTML('afterEnd','<input type="hidden" class="calcr" style="display:none;"/>');
    var calcr = document.querySelectorAll('.calcr');
    
    function doCalc(){
        calcr[i].value = calc[i].value;
        if(calc[i].value != ''){
            if(calc[i].getAttribute("data")){
                var data = calc[i].getAttribute("data").split("-");
                if(data[0]==="N"){
                    calc[i].value = eval(calc[i].value.replace(/\,/g, ""))===undefined?'':eval(calc[i].value.replace(/\,/g, ""));
                    if(data[1] != null && data[1] != ""){calc[i].value = parseFloat(calc[i].value).toFixed(data[1]);}
                    calc[i].value = calc[i].value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
                }else if(data[0]==="S"){
                    calc[i].value = eval(calc[i].value.replace(/\./g, "").replace(/\,/g, "."))===undefined?'':eval(calc[i].value.replace(/\./g, "").replace(/\,/g, "."));
                    if(data[1] != null && data[1] != ""){calc[i].value = parseFloat(calc[i].value).toFixed(data[1]);}
                    calc[i].value = calc[i].value.toString().replace(/\./g, ",").replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");
                }
            }else{
                calc[i].value = eval(calc[i].value)===undefined?'':eval(calc[i].value);
            }
        }
    }
    function mouseDown(){
        if(calcr[i].value != ''){
            calc[i].value = calcr[i].value;enKey = false;
        }
    }
    function onBlur(){
        if(enKey){enKey = false;}else{doCalc();}
    }
    
    el.addEventListener('mousedown',mouseDown);
    el.addEventListener('blur',onBlur);
    el.addEventListener('keypress',function(e){
    var key = e.which || e.keyCode;
        if(key === 13){
            enKey = true;
            doCalc();
        }
    });
});
// Label for copy
var calcc = document.querySelectorAll('.calcc');
var forEach = [].forEach;
forEach.call(calcc, function (el, i) {
    function mouseEnter(){
        calcc[i].innerHTML = calc[i].value;
        calcc[i].style.padding = "6px 6px 6px 5px";
    }
    function mouseOut(){
        calcc[i].innerHTML = "c";
        calcc[i].style.padding = "0px 2px 0px 2px";
    }
el.addEventListener('mouseenter',mouseEnter);
el.addEventListener('mouseout',mouseOut);
});

//save file
function downloadCalcInline(){
    var textToWrite = document.getElementById('inlineCalcJs').innerHTML.replace(/\&lt;/g, "<").replace(/\&gt;/g, ">").replace(/\&amp;/g, "&");
    var textFileAsBlob = new Blob([textToWrite], {type:'text/javascript'});
    var downloadLink = document.createElement("a");
    downloadLink.download = "inline-calc.js";
    downloadLink.innerHTML = "Download File";
    
    if (window.webkitURL != null){
        // Chrome allows the link to be clicked
        // without actually adding it to the DOM.
        downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob); 
    }else{
        // Firefox requires the link to be added to the DOM
        // before it can be clicked.
        downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
        downloadLink.onclick = destroyClickedElement;
        downloadLink.style.display = "none";
        document.body.appendChild(downloadLink);
    }
    downloadLink.click();
}

function destroyClickedElement(event){
    document.body.removeChild(event.target);
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console