Solved

Updating a value when checkbox is unchecked

Posted on 2013-01-03
14
459 Views
Last Modified: 2013-01-03
I have a checkbox in a grid and for its OnClick event, i call a javascript that adds values together. This section of it works.

Now, even when I uncheck the checkbox, the javascript is called and values are still added. I dont want to add when  checkbox is unchecked...i want to decrement...

Not sure how to fix this;

<asp:CheckBox runat="server" onclick='<%# String.Format("javascript:prices(\"{0}\")", Eval("price")) %>'     ID="chkSelect" />

<script type="text/javascript">
     var amount = 0.00;
     function prices(price) {

       
         var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
          // alert(tempAmount.value);
         amount = parseFloat(price) + parseFloat(tempAmount.value);
        //  alert(amount);

         tempAmount.value = parseFloat(amount);
         // alert(amount.value);
        // return false;


     }
  </script>

Open in new window

0
Comment
Question by:Camillia
  • 8
  • 5
14 Comments
 
LVL 12

Expert Comment

by:TomRScott
ID: 38741463
Once the form is posted, the value of the check box(s) can be evaluated and you can increment/decrement based on that/those value(s) at that time.

However, I think what you really should use is a pair of buttons "+" and "-".

 - Tom
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38741503
replace : <asp:CheckBox runat="server" onclick='<%# String.Format("javascript:prices(\"{0}\")", Eval("price")) %>'     ID="chkSelect" />
by : <asp:CheckBox runat="server" onclick='prices(this)' value='<%# Eval("price") >'    ID="chkSelect" />

and use :
<script type="text/javascript">
	var amount = 0.00;
	function prices(checkbox) {	
		var v = checkbox.checked?checkbox.value:-checkbox.value;
		var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
		// alert(tempAmount.value);
		amount = parseFloat(checkbox.value) + parseFloat(tempAmount.value);
		//  alert(amount);
		tempAmount.value = parseFloat(amount);
		// alert(amount.value);
		// return false;
	}
</script>

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 38741732
I get NaN for value. I put alerts to debug and I click on a checkbox and these are the values I see...not sure why i get 0.00

 <script type="text/javascript">     var amount = 0.00;
     function prices(checkbox) {

         alert(checkbox); //I get 50 ...the value i passed
         var v = checkbox.checked ? checkbox.value : -checkbox.value;
         var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
          alert(tempAmount.value); //i get 0.00
         amount = parseFloat(checkbox.value) + parseFloat(tempAmount.value);
           alert(amount); //I get NaN
         tempAmount.value = parseFloat(amount);
         alert(tempAmount.value); //I get NaN
         // return false;
     }
</script>
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 82

Expert Comment

by:leakim971
ID: 38741745

  alert(checkbox); //I get 50 ...the value i passed

You MUST have :
<asp:CheckBox runat="server" onclick='prices(this)' value='<%# Eval("price") >'    ID="chkSelect" />
0
 
LVL 7

Author Comment

by:Camillia
ID: 38741750
1. Ok, I got the NaN figured out but still doesnt work. I used your Javascript with my line of checkbox:
 <asp:CheckBox runat="server" onclick='<%# String.Format("javascript:prices(\"{0}\")", Eval("price")) %>'     ID="chkSelect" />

This got rid of the NaN value but still it doesnt decrement

2. I used your Javascript with our line of checkbox
<asp:CheckBox runat="server" onclick='prices(this)' value='<%# Eval("Price") %>'      ID="chkSelect" />

I get "NaN" for values. The first alert gives me "InputHTMLElement", then 0.00, then NaN, NaN.

I think it's the checkbox Value that's not being passed?
0
 
LVL 7

Author Comment

by:Camillia
ID: 38741770
asp.net's checkbox doesnt have "value" as a property. I wonder if I need to use this

  <checkbox  runat="server" onclick='prices(this)' value='<%# Eval("Price") %>'      ID="chkSelect"></checkbox>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38741776
The first alert gives me "InputHTMLElement"(good), then 0.00(good) , then NaN, NaN

look like the eval don't work...
what do you get with :

function prices(checkbox) {
alert(checkbox.value); // add this to display the value of the checkbox

?
0
 
LVL 7

Author Comment

by:Camillia
ID: 38741807
I get "on" (no double quotes)

 I have this on the same page and this works

<asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price") %>'></asp:Label>

...i wonder if I should change "asp:checkbox" to checkbox
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38741808
another one :
<asp:CheckBox runat="server" onclick='<%# String.Format("javascript:prices(this,\"{0}\")", Eval("price")) %>'     ID="chkSelect" />
with :
<script type="text/javascript">
	var amount = 0.00;
	function prices(checkbox, v) {
		v = checkbox.checked?v.value:-v.value;
		var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
		// alert(tempAmount.value);
		amount = parseFloat(v) + parseFloat(tempAmount.value);
		//  alert(amount);
		tempAmount.value = parseFloat(amount);
		// alert(amount.value);
		// return false;
	}
</script>

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 38742170
let me try and i will post back
0
 
LVL 7

Author Comment

by:Camillia
ID: 38742617
I still get NaN:

  alert(checkbox); //HTMLInputElement
        alert(v);  //i get 50
        v = checkbox.checked ? v.value : -v.value;
        var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
        alert(tempAmount.value); //0.00
        // alert(tempAmount.value);
        amount = parseFloat(v) + parseFloat(tempAmount.value);
         alert(amount); //NaN
        tempAmount.value = parseFloat(amount);
        alert(tempAmount.value); //NaN
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38742627
corrected :
<script type="text/javascript">
	var amount = 0.00;
	function prices(checkbox, v) {
		v = checkbox.checked?v:-v;
		var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
		// alert(tempAmount.value);
		amount = parseFloat(v) + parseFloat(tempAmount.value);
		//  alert(amount);
		tempAmount.value = parseFloat(amount);
		// alert(amount.value);
		// return false;
	}
</script>

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 38742632
This comes as NaN

 alert(parseFloat(v));  //NaN
        alert(parseFloat(tempAmount.value)); //this is zero
0
 
LVL 7

Author Comment

by:Camillia
ID: 38742634
We posted at the same time. Let me try the one u posted
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question