Skip to content

Instantly share code, notes, and snippets.

@agungsijawir
Last active December 17, 2015 06:58
Show Gist options
  • Save agungsijawir/5569062 to your computer and use it in GitHub Desktop.
Save agungsijawir/5569062 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="ID">
<head>
<title>OnChange Function JavaScript</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
</head>
<body>
<input type="text" name="text1" value="0"/> <br/>
<input type="text" name="text2" value="0"/> <br/>
<input type="text" name="text3" value="0"/> <br/>
<span id="sum">0</span> <br/>
Note: <span id="note">0</span>
<script>
$(document).ready(function(){
$('input[name="text1"]').keyup(function(){
var text1Value = parseInt($('input[name="text1"]').val());
var text2Value = parseInt($('input[name="text2"]').val());
var text3Value = parseInt($('input[name="text3"]').val());
var total = text1Value + text2Value + text3Value;
if(text1Value > 100) {
$('input[name="text1"]').val(100);
$('input[name="text2"]').val(''); // awal: $('input[name="text2"]').val(0);
$('input[name="text3"]').val(''); // awal: $('input[name="text3"]').val(0);
//$('input[name="text2"]').attr('disabled','disabled');
//$('input[name="text3"]').attr('disabled','disabled');
} else {
// exception: text2
$('input[name="text2"]').val(100 - text1Value - text3Value);
$('input[name="text2"]').removeAttr('disabled');
$('input[name="text3"]').removeAttr('disabled');
}
});
$('input[name="text2"]').keyup(function(){
var text1Value = parseInt($('input[name="text1"]').val());
var text2Value = parseInt($('input[name="text2"]').val());
if(isNaN(text1Value)) {
text1Value = 0;
}
if(isNaN(text2Value)) {
text2Value = 0;
}
var total = text1Value + text2Value;
if(total > 100) {
$('input[name="text2"]').val(100-text1Value);
$('input[name="text3"]').val(''); // awalnya seperti ini >> $('input[name="text3"]').val('');
// yang ini saya comment
//$('input[name="text3"]').attr('disabled','disabled');
} else {
$('input[name="text3"]').removeAttr('disabled');
$('input[name="text3"]').val(100-total);
}
});
$('input[name="text3"]').keyup(function(){
var text1Value = parseInt($('input[name="text1"]').val());
var text2Value = parseInt($('input[name="text2"]').val());
var text3Value = parseInt($('input[name="text3"]').val());
if(isNaN(text1Value)) {
text1Value = 0;
}
if(isNaN(text2Value)) {
text2Value = 0;
}
var total = text1Value + text2Value + text3Value;
if(total > 100) {
$('input[name="text2"]').val(100 - text1Value - text3Value);
//$('input[name="text3"]').val(text3Value-text2Value); // awalnya seperti ini >> $('input[name="text3"]').val('');
//$('input[name="text3"]').attr('disabled','disabled');
} else {
// $('input[name="text2"]').removeAttr('disabled');
$('input[name="text2"]').val(100 - text1Value - text3Value);
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment