Solved

Help with jquery to change value in textbox control

Posted on 2014-12-08
4
159 Views
Last Modified: 2014-12-08
I have  a checkbox called cboFee and a textbox called txtAmount.   When someone clicks on the checkbox I need to add $20.00 to the amount field.  if the checkbox is unchecked I need to subtract from it.  This needs to be done in jquery because I don't want to do  a call back.   Could someone give me sample code and how this would work.

thanks
0
Comment
Question by:mgmhicks
  • 2
  • 2
4 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40487171
$("#checkboxid").change(function(){
    if($(this).is(":checked")){
        $("#textboxid").val(parseFloat($("#textboxid").val()+20))
    }
    else{
        $("#textboxid").val(parseFloat($("#textboxid").val()-20))
    }
})

Open in new window

0
 

Author Comment

by:mgmhicks
ID: 40487282
this is what I have because of master page

$("#<%=cbxfee.ClientID  %>").change(function() {
    if ($(this).is(":checked")) {
        $("#<%=txtAmt.ClientID %>").val(parseFloat($("#<%=txtAmt.ClientID %>").val() + 20))
    }
    else {
        $("#<%=txtAmt.ClientID %>").val(parseFloat($("#<%=txtAmt.ClientID %>").val() - 20))
    }
})
                  
            });
seems like when it adds it appends the value put when it subtracts it does subtract it. I start off with 0.00 , check the box and I get 0.002 then I click it again I get, -19.998

so we are close
thanks
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40487290
Had the brackets in the wrong place

$("#<%=cbxfee.ClientID  %>").change(function() {
     if ($(this).is(":checked")) {
         $("#<%=txtAmt.ClientID %>").val(parseFloat($("#<%=txtAmt.ClientID %>").val()) + 20)
     }
     else {
         $("#<%=txtAmt.ClientID %>").val(parseFloat($("#<%=txtAmt.ClientID %>").val()) - 20)
     }
 })
                   
             });

Open in new window

0
 

Author Closing Comment

by:mgmhicks
ID: 40487303
thank you
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
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…

830 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