tags:

views:

70

answers:

2

Hello, I have this jquery code, and want to toggle it. When I use

$('#checkb').toggle(function() });

It does not toggle

$('#checkb').bind('click',function() {
    var PackagePrice = $('#specs_packageprice').text();
    var Quantity = $('#specs_quantity').text();
    var ItemPrice = $('#specs_itempr').text();
    var Unit = $('#specs_unit').text();     
    var calc = Unit*ItemPrice;

    $('#final_value').text(calc);                               
});

What could be wrong?

Thanks Jean

+3  A: 
$('#checkb').toggle( function () { // Every even click
  var PackagePrice = $('#specs_packageprice').text();
  var Quantity = $('#specs_quantity').text();
  var ItemPrice = $('#specs_itempr').text();
  var Unit = $('#specs_unit').text();     
  var calc = Unit*ItemPrice;
  $('#final_value').text(calc);                               
},
function () { // Every odd click
  // Do something here
});

EDIT : Toogle seems to be messing with checkbox. You can try

$('#checkb').click( function () {
  if ($(this).attr("checked")) {
    //do stuff if the checkbox is checked
  } else {
    //do stuff if the checkbox isn't checked
  }

);

Loïc Février
Here #checkb is the id of the checkbox. It works fine, but the checkbox is not getting checked
Jean
+1  A: 

set up a function to handle this like so:

function calc_on_click(){
    var PackagePrice = $('#specs_packageprice').text();
    var Quantity = $('#specs_quantity').text();

    var ItemPrice = $('#specs_itempr').text();
    var Unit = $('#specs_unit').text();     
    var calc = Unit*ItemPrice;

    $('#final_value').text(calc);

    return false;
}

then just bind a click element to your sum button!

$("#checkb").click(calc_on_click);
//Or if its explicitly a toggle needed:
$("#checkb").toggle(calc_on_click,calc_on_click); //On / Off

Then that fuction should do what you need it to do

RobertPitt