Solved

Updating a value when checkbox is unchecked

Posted on 2013-01-03
14
458 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i extend ? 2 45
https://github.com/willowsystems/jSignature 11 52
Hide cell in a table 2 15
Multiflying 2 Input Text On a Table 7 22
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

832 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