views:

35

answers:

4

Hi there,

I have a classic asp form to add products to a site. The form has 3 fields of 'Price', 'Sale' Price and a checkbox for 'On Sale'.

What i'm trying to do is, when a user enters a 'Price' and ticks the 'On Sale' checkbox, some javascript would complete the 'Sale Price' with a value that is 'Price' - 10%.

Wondering if anyone may know how to do this? :D

Thank you.

A: 

Would be easier with the HTML ;) , something like that would do the trick:

$('#yourCheckBox').change(function(){
  if($("#yourCheckBox option:selected").length){
    $('#yourSalePrice').val(
      $('#yourPrice') - (($('#yourPrice').val()/100)*10)
    );
  }
});

You could also add some check if theres a Value in Price before calculating anything

Hannes
A: 

HTML:

Price:<br>
<input type="text" name="price" id="price"><br>
<br>
Sale price:<br>
<input type="text" name="sale_price" id="sale_price"><br>
<br>
<input type="checkbox" name="on_sale" onchange="calculate_sale_price(this);"> On sale

Javascript:

function calculate_sale_price(checkbox)
{
    price = document.getElementById('price');
    sale_price = document.getElementById('sale_price');
    sale_price.value = checkbox.checked ? price.value * 0.9 : price.value;
}

If you want sale price to be empty if the checkbox is not checked, change this line:

sale_price.value = checkbox.checked ? price.value * 0.9 : price.value;

to:

sale_price.value = checkbox.checked ? price.value * 0.9 : '';
captaintokyo
Thank you very much. :D Would it be easy enough to get the sale_price field to just be empty if the checkbox is not checked, instead of putting in the price? :)
Neil Bradley
Yes, it's easy to make it empty if the checkbox is not checked. See my updated answer above.
captaintokyo
cm2
Awesome. Thank you very much. :)
Neil Bradley
I would personally recommend using a listener so that the code is easier to update later should you decide to change the way things work.
cm2
A: 

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"&gt;&lt;/script&gt;

  <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"&gt;&lt;/script&gt;
<![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

David Thomas
Thank you very much. :D Would it be easy enough to get the sale_price field to just be empty if the checkbox is not checked, instead of putting in the price? :)
Neil Bradley
Do you mean empty *before* the checkbox is checked? Or in some other way?
David Thomas
Hey David. As in if they uncheck the checkbox again, the sale price field could go back to empty.
Neil Bradley
@Neil, yeah, it's easy. See edit.
David Thomas
Awesome. Thank you. All the answers here have been great for different reasons. Yours for going the html5. :D
Neil Bradley
@Neil, it's always a pleasure. Feel free to up-vote any answers you find useful. =)
David Thomas
A: 

As others have shown through their code, you can do this by adding an event listener to the checkbox. It's clean and doesn't get in the way of the ASP form code. Since you put jQuery, here's my take on it; there are of course other solutions but I feel like this an okay combination of concise and readable. Good luck!

   $('#chkbox').bind('click', function(evt){
       var sale = (evt.target.checked);
       var price = $('#fullprice').val();
       var disc = (+price > 0 && sale ) ? price*.9 : ''
       $('#saleprice').val( disc );
    });
cm2