+ Start a Discussion

How do i get value of checkbox check and then uncheck with javascript or jquery


I have number of records displayed in the form of table with checkbox.
what i have to do is firstly i select a checkbox and getting its amount and subtracting it from user's entered amount. then i select 1 more record and subtracting it the same way. but if i deselect the first checkbox then i want the same old amount in its field.
How can i achieve this. 
I assume your requirements is pure javascript solution.

1) in jquery register click handlers of checkboxes.
2) in click handler write mnipulation logic of changing the value of calculated amount.

Try to write javascript by your own, come back here if you face any issue.
Hi Ashish,

I have tried it 
check this.

function selectItem(oid)
var newAmt;
var isSelect = true;
//Amount Entered by user
var amountRemng = document.getElementById('page:form:theblockGrid:section1:item3:amountRem').innerHTML*1;
  if(jQuery(this).attr('id') == oid && jQuery(this).attr('checked'))
   var amnt = $(this).closest('tr').find('.amountname').text();   //Amount in table
   var amntsplit = amnt.replace('$',"").trim();
   var amntrep = amntsplit.replace(',',"").trim()*1;
   if(amountRemng > amntrep)
    document.getElementById('page:form:theblockGrid:section1:item3:amountRem').innerHTML = (amountRemng - amntrep).toFixed(2);
    document.getElementById('page:form:theblockGrid:section1:item3:amountRem').innerHTML = 0;
    $(this).closest('tr').find('.amountname').text((amntrep - amountRemng).toFixed(2));

But what if checkbox is unchecked from checked 
for that i need old value 
how can i get that

Write another function to hold your manipulation logic and use another If condition to check if checkbox is deselected, like below
if(jQuery(this).attr('id') == oid && !(jQuery(this).attr('checked')))
Hi Ashish,

I already tried that but it is giving me new value not the old one.

user entered amount = 2000
Amount = 2000

In table i have selected 1 record and its amount is 1616
now amount remaining will be 384 and amount in table will be 0     // this is according to the condition that i have used in the code before

now i select second record and its amount is 3000
Amount Remaining will be 0 and amount in table will be 2616( 3000- 384)

now remaining amount is 0 and if i deselect first checkbox then it is giving me, 0 value , new one but i want to set old value if checkbox is unchecked

Please advise
hmm .. pretty interesting. I would suggest you following approach, 

Take an array of size (and fill value with zero - preferably) equal to number of checkboxes. whenever you make modifition to remaining amount, store it in the place in array at index equal to checkbox index. Now for retrieveing old values retrieve it with the index of checkbox which got deselected.

Thanks Ashish
can you give me example code. I am totally new to jquery and javascript