Ghufron Asadly. Diberdayakan oleh Blogger.

Membuat Kalkulator sederhana dengan JS

Kamis, 19 Mei 2016




1. Cara Pertama
<script>     function kalkulatorTambah(type1, type2) {         var hasil = eval(type1) * eval(type2);         var h=document.getElementById('resulte');         h.value=hasil; </script>
<input type="text" id="type1" onkeyup="kalkulatorTambah(this.value, getElementById('type2').value);" />x
    <input type="text" id="type2" onkeyup="kalkulatorTambah(getElementById('type1').value, this.value);" />
    =
    
        <input type="text" name="a" id="resulte" onkeyup="kalkulatorTambah(getElementById('resulte').hasil);">
    
2.Cara Yang Ke 2
<script>
    function kalkulatorTambah(type1, type2) {
        var hasil = eval(type1) * eval(type2);
      document.getElementById('resulte').innerHTML = hasil;
    }
</script>


<input type="text" id="type1" onkeyup="kalkulatorTambah(this.value, getElementById('type2').value);" />x
    <input type="text" id="type2" onkeyup="kalkulatorTambah(getElementById('type1').value, this.value);" />
    =
    <span id="resulte">        
    </span>
3. cara Yang Ke 3
<script type="text/javascript">

function calculate(){
var result = document.getElementById('result');
var el, i = 0, total = 0;
while(el = document.getElementById('v'+(i++)) ) {
el.value = el.value.replace(/\\D/,"");
total = total + Number(el.value);
}
result.value = total;
if(document.getElementById('v0').value =="" && document.getElementById('v1').value =="" && document.getElementById('v2').value =="" ){
 result.value ="";
}
}
</script>
Some number:<input type="text" id ="v0" onkeyup="calculate()"><br>
Some number:<input type="text" id ="v1" onkeyup="calculate()"><br>
Some number:<input type="text" id ="v2" onkeyup="calculate()"><br>
Result: <input type="text" id="result" onkeyup="calculate()"  readonly><br>

Tidak ada komentar:

Posting Komentar

 

Most Reading