Skip to content

Instantly share code, notes, and snippets.

@MauroJr
Created June 9, 2017 15:06
Show Gist options
  • Select an option

  • Save MauroJr/1572afa23f569feef461126a3e1e6961 to your computer and use it in GitHub Desktop.

Select an option

Save MauroJr/1572afa23f569feef461126a3e1e6961 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form id="form">
<input type="text">
<input type="text">
<input data-action="multi" type="submit" value="Multiplicar">
<input data-action="div" type="submit" value="Dividir">
</form>
<p id="result"></p>
<script>
(function () {
var form = document.getElementById('form');
var val1 = form[0];
var val2 = form[1];
var result = document.getElementById('result');
form.addEventListener('submit', handleSubmit);
form.addEventListener('click', handleClick);
function handleSubmit(evt) {
evt.preventDefault();
}
function handleClick(evt) {
var target = evt.target;
var number1 = parseFloat(val1.value);
var number2 = parseFloat(val2.value);
switch (target.dataset.action) {
case 'multi': return result.textContent = number1 * number2;
case 'div': return result.textContent = number1 / number2;
}
}
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment