Saturday, March 5, 2011

Java Script+DHTML..

Pembuatan aplikasi sederhana dalam web yang interaktif dengan memanfaatkan HTML dinamis (DHTML) serta Java Script.
Hasilnya seperti:


Codenya:

<!DOCTYPE html>
<html lang="en">
<head>

<title>Kalkulator Sederhana</title>

</head>

<body>

<script language="JavaScript" type="text/javascript">

function hitung(){

var myForm = document.form1;

var x=eval(myForm.x.value);

var y=eval(myForm.y.value);

var pil= myForm.opt.value;

if (pil == "tambah") {

    var z = x + y;

 }else if (pil == "kurang") {

    var z = x - y;

 } else if (pil == "kali") {

    var z = x * y;

 } else if (pil == "bagi") {

    var z = x / y;

 }

 myForm.hasil.value = z;

 myForm.x.value = "";

 myForm.y.value = "";

}

function resetForm(){

document.form1.reset();

}

</script>

<form name="form1" action="#">

<input type="text" name="x" />

<select name="opt">

 <option value="tambah"> + </option>

 <option value="kurang"> - </option>

 <option value="kali"> * </option>

 <option value="bagi"> / </option>

</select>

<input type="text" name="y" />

<input type="button" value="=" onClick="hitung()" />

<input type="text" name="hasil" disabled="true" /><br/>

<input type="button" value="RESET" onClick="resetForm()" />

</form>

</body>

</html>

No comments:

Post a Comment