Solved

Updating a value when checkbox is unchecked

Posted on 2013-01-03
14
461 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

733 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