Question

I am loading a data using jQuery load method. It works fine. Now I want to multiple it with 2. eg If i have 1000 I want to multiple it with 2 so that it will become 2000. It works fine only when I include alert in my code. If i remove alert, it returns 1000.

here is my code

$('#<%= qnty.ClientID %>').click(function () {


            var desID = $('#<%= designID.ClientID %>').val();
            var qnty = $('#<%= qnty.ClientID %>').val();

            $("#amountHave").load("AmountRetrieve.aspx?desID=" + desID + "&qnty=" + qnty + " #amountHaveRet");
                if ($("#reverseDes").is(":checked") || $("#plain").is(":checked")) {
                    var slash = ".00/-";
                    var getAmnt = $("#amountHave").text();
                    getAmnt = getAmnt.replace(slash, '');

                    var finamAmount = parseInt(getAmnt, 10) + parseInt(getAmnt, 10);                    
                    //IF I USE ALERT HERE IT WORKS
                    //alert(finamAmount)
                    $("#amountHave").text(finamAmount + slash);

                }                   
                else {
                    $("#blackholder").text("-------- Rs. " + $("#<%= qnty.ClientID %>").val());
                    $("#matchholder").text("-------- Rs. " + $("#<%= qnty.ClientID %>").val());

                }


        });

Any help would be highly appreciated.

Was it helpful?

Solution

You forgot your callback:

$("#amountHave").load("AmountRetrieve.aspx?desID=" + desID + "&qnty=" + qnty + " #amountHaveRet", function(){
    if ($("#reverseDes").is(":checked") || $("#plain").is(":checked")) {
        var slash = ".00/-";
        var getAmnt = $("#amountHave").text();
        getAmnt = getAmnt.replace(slash, '');
        var finamAmount = parseInt(getAmnt, 10) + parseInt(getAmnt, 10);
        $("#amountHave").text(finamAmount + slash);
    }                   
    else {
        $("#blackholder").text("-------- Rs. " + $("#<%= qnty.ClientID %>").val());
        $("#matchholder").text("-------- Rs. " + $("#<%= qnty.ClientID %>").val());
    }
});

OTHER TIPS

Since you are using ajax (asynchronous) you have to wait till the server responds, therefore you have to put your code in a callback function which will be invoked when the server responds.

The text wasn't set because the code was already executed before the server had the time to respond, those extra seconds for showing the alert were buying the server enough time to respond.

 $("#amountHave").load("AmountRetrieve.aspx?desID=" + desID + "&qnty=" + qnty + " #amountHaveRet", function () {
     if ($("#reverseDes").is(":checked") || $("#plain").is(":checked")) {
         var slash = ".00/-";
         var getAmnt = $("#amountHave").text();
         getAmnt = getAmnt.replace(slash, '');

         var finamAmount = parseInt(getAmnt, 10) + parseInt(getAmnt, 10);
         //IF I USE ALERT HERE IT WORKS
         //alert(finamAmount)
         $("#amountHave").text(finamAmount + slash);

     } else {
         $("#blackholder").text("-------- Rs. " + $("#<%= qnty.ClientID %>").val());
         $("#matchholder").text("-------- Rs. " + $("#<%= qnty.ClientID %>").val());

     }
 });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top