0

I just can't figure out why my script isn't working. I tried several solutions I found here, also tried using .ajax(), but no results.

I must've missed something because it's not the first time I'm using .get or making something similar.

<div class="test"></div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
    $(function() {
        var btc = getBTC(10);

        $('.test').html(btc);
    });

    function getBTC(val) {
        $.get("https://blockchain.info/tobtc?currency=EUR&value=" + val, function(data) {
            return data;
        });
    }
</script>

If I change return data; to console.log(data); then execute getBTC(10); in the console it works fine. I'm a little bit confused here.

imre Boersma
  • 71
  • 1
  • 9
Mike
  • 23
  • 4

1 Answers1

0
<script>
   $.get("https://blockchain.info/tobtc?currency=EUR&value=" + val, function(data) {
     $('.test').html(data);
   });
</script>

Your code is asynchronous. This means that the response is not ready yet. You have to wait for it. Only then you can use data as a value.

M1X
  • 4,971
  • 10
  • 61
  • 123