0

What am I missing here? My jquery function isn't firing. Here's the HTML:

<div id="sum_income" class="col-sm-3" onload="sumIncome();">                     

</div>

and here's the jquery function:

function sumIncome() {

var cur_month = $('#month option:selected').attr("value");

$.ajax({
        type: "POST",
        url: 'inc/functions.php',
        data: {action: "get_sum_income", cur_month:cur_month},
        success: function(response){
            $('#sum_income').html(response);
            setTimeout(sumIncome, 5000);
                }
    })
};
4
  • What error you are getting in your console please write here the exact error of your console. Commented Dec 2, 2013 at 13:14
  • try this stackoverflow.com/questions/4057236/… Commented Dec 2, 2013 at 13:16
  • @NeerajKumar he's getting no error, I'm sure. Commented Dec 2, 2013 at 13:19
  • @DontVoteMeDowm i have written the code which will work definetly Commented Dec 2, 2013 at 13:30

3 Answers 3

3

change your jquery code.

 $(document).ready(function(){
            if($("#sum_income").length){
                var cur_month = $('#month option:selected').attr("value");

            $.ajax({
              type: "POST",
              url: 'inc/functions.php',
              data: {action: "get_sum_income", cur_month:cur_month},
              success: function(response){
              $('#sum_income').html(response);
                 setTimeout(sumIncome, 5000);
               }
            })
          }
        });

This will work definetly

Sign up to request clarification or add additional context in comments.

Comments

2

Use $(window).onload(); instead of adding it to any HTML elements.

<script> 
  $(window).onload(function () {
    sumIncome();
  });
</script>

Comments

1

html:

<body onload="sumIncome();">
</body>

js:

function sumIncome() {

    var m = $('#month option').val(); // assuming this is a selected tag

    var request = $.ajax({
        type: "POST",
        url: 'inc/functions.php',
        data: {action: "get_sum_income", cur_month:m},
        dataType: "html"
    });
    request.done(function( response ) {
        $('#sum_income').html(response);
    });
    request.fail(function( jqXHR, textStatus ) {
        alert( "Request failed: " + textStatus );
    });
    request.done(function() {
        setTimeout(sumIncome, 5000);
    })

}

PS: No onload method for divs.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.