I've posted a demo over at: jsbin, based on the following markup:
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
$('#onSale').change(
function() {
var salePrice = $('#price').val() * 0.9;
$('#salePrice').val(salePrice);
}
);
}
);
</script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<form method="post" action="">
<fieldset>
<label for="price">Price:</label>
<input type="text" id="price" name="price" />
<input type="text" name="salePrice" id="salePrice" disabled="disabled" />
<input type="checkbox" name="onSale" id="onSale" />
</fieldset>
</form>
</body>
</html>
I'd suggest that you'd need to build in validation for the price entered by the user (make sure it's a numerical price, for a start). I've made the #salePrice
disabled so that users are less likely to be able to enter data (though it's not impossible).
Edited in response to OP's comment.
To clear the field after the user un-ticks the checkbox:
$(document).ready(
function() {
$('#onSale').change(
function() {
if ($('#onSale').is(':checked')) {
var salePrice = $('#price').val() * 0.9;
$('#salePrice').val(salePrice);
}
else {
$('#salePrice').val('');
}
}
);
}
);
Note the new if/else
, and here's the revised demo