Solved

Updating a value when checkbox is unchecked

Posted on 2013-01-03
14
455 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now