locked
How do i call same function on page load in jquery RRS feed

  • Question

  • User-1821202846 posted

    I am wanting to calculate totals on page load of a webforms page I am using jquery to cacluate when the user types into a field this works but now I need the totals to display on page load. also need to be able to  create this forumla of

    iconmesum - (expendureSum + reducibleSumTotal)

    How would i go about doing that in jquery

    <script>
    	$(document).ready(function(){
    
    		//iterate through each textboxes and add keyup
    		//handler to trigger sum event
    	    $(".incomeSum").each(function () {
    
    			$(this).keyup(function(){
    				calculateSum();
    			});
    	    });
    
    
    	    $(".expendureSum").each(function () {
    
    	        $(this).keyup(function () {
    	            expendureSum();
    	        });
    	    });
    
    
    
    	    $(".reducibleSum").each(function () {
    
    	        $(this).keyup(function () {
    	            reducibleSumCalc();
    	        });
    	    });
    
    	    
    	});
    	function calculateSum() {
    
    	    var sum = 0;
    	    //iterate through each textboxes and add the values
    	    $(".incomeSum").each(function () {
    
    	        //add only if the value is number
    	        if (!isNaN(this.value) && this.value.length != 0) {
    	            sum += parseFloat(this.value);
    	        }
    
    	    });
    	    //.toFixed() method will roundoff the final sum to 2 decimal places
    	    $("#sumTotalIncome").html(sum.toFixed(2));
    
    
    	}
    
    	function expendureSum() {
    
    	    var sum = 0;
    	    //iterate through each textboxes and add the values
    	    $(".expendureSum").each(function () {
    
    	        //add only if the value is number
    	        if (!isNaN(this.value) && this.value.length != 0) {
    	            sum += parseFloat(this.value);
    	        }
    
    	    });
    	    //.toFixed() method will roundoff the final sum to 2 decimal places
    	    $("#expendureTotalSum").html(sum.toFixed(2));
    
    
    	}
     
    	  
    	function reducibleSumCalc() {
    
    	    var sum = 0;
    	    //iterate through each textboxes and add the values
    	    $(".reducibleSum").each(function () {
    
    	        //add only if the value is number
    	        if (!isNaN(this.value) && this.value.length != 0) {
    	            sum += parseFloat(this.value);
    	        }
    
    	    });
    	    //.toFixed() method will roundoff the final sum to 2 decimal places
    	    $("#reducibleSumTotal").html(sum.toFixed(2));
    
    
    	}
     
     
    
    
    	
            </script>

    Also how do i get the values from c# code behind i tried runat=server on the span but then my jquery stoped working the total span has a field like such

    <span id="sumTotalIncome" >0</span>



    Wednesday, August 17, 2016 6:56 AM

Answers

  • User-271186128 posted

    Hi rogue1,

    how do i get the values from c# code behind i tried runat=server on the span but then my jquery stoped working the total span has a field like such

    You could add a Asp.net HiddenField control, after calculating the result, you could assign the value to the hidden field.

    Then, from code behind, you could get the hidden field value from code behind.

    Note, when use HiddenField value, make sure you have add the value property. Code as below:

          <asp:HiddenField ID="HiddenField1" runat="server" Value="" />

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 17, 2016 10:53 AM

All replies

  • User702547207 posted

    You can refer the below link for returning the value from jquery function.

    http://stackoverflow.com/questions/6861039/jquery-function-return-value

    Once you return the value with jquery then you can do the calculation you need.

    Wednesday, August 17, 2016 10:37 AM
  • User-271186128 posted

    Hi rogue1,

    how do i get the values from c# code behind i tried runat=server on the span but then my jquery stoped working the total span has a field like such

    You could add a Asp.net HiddenField control, after calculating the result, you could assign the value to the hidden field.

    Then, from code behind, you could get the hidden field value from code behind.

    Note, when use HiddenField value, make sure you have add the value property. Code as below:

          <asp:HiddenField ID="HiddenField1" runat="server" Value="" />

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 17, 2016 10:53 AM