-1

I know this has been asked a million times, but all I see is people explaining scope and no one explains a solution.

In this case HOW do I get alert to show the value of comment_count rather than undefined.

 var comment_count;
 $.post( "<?=site_url('comments/ajax_get_comment_count');?>",{tags:[aData[7],'BWQ']}, function( data ) {

  comment_count = data;

  });

  alert(comment_count);
PrestonDocks
  • 4,851
  • 9
  • 47
  • 82
  • 2
    If you know there's a lot of questions (and answers) for this question, why not read them? – Etheryte Sep 04 '15 at 11:16
  • @nit, I would suggest you read my question again. You will see that I can only find explanations for for how scope works, but no examples of how to use a variable outside a $.post function. – PrestonDocks Sep 06 '15 at 13:11
  • Then it simply seems you're not that good at reading answers. – Etheryte Sep 06 '15 at 14:08

1 Answers1

-1

I think you need something like that:

 var comment_count;

function controller(){


    return {
        setValue:function(_val,data){
           _val = data;
            this.fireValue(_val)
        },
        fireValue:function(_val){
            alert(_val)   
        }
    }

}

$.post("<?=site_url('comments/ajax_get_comment_count');?>", {tags: [aData[7], 'BWQ']}, function (data) {

    controller.setValue(comment_count,data)

});
guvenckardas
  • 738
  • 4
  • 8