0

I have function like that

function cryptChange(cr){
        var url = 'https://min-api.cryptocompare.com/data/dayAvg?fsym=' + cr + '&tsym=PLN';
        console.log(url);      // it's just for testing in console

};
cryptChange('LTC');
cryptChange('BTC');

As you can see this code is working fine with URL of AJAX call with JSON data, returning valid URL. Want to make something like that, but in shorter version, I have many lines of code like the ones below and I do want to get less

$.getJSON('https://min-api.cryptocompare.com/data/dayAvg?fsym=BTC&tsym=PLN', function(btc2){
    $('#tt-11').html(btc2.PLN.toFixed(2)); //its passed into html block
});
$.getJSON('https://min-api.cryptocompare.com/data/dayAvg?fsym=BCH&tsym=PLN', function(bch2){
        $('#tt-12').html(bch2.PLN.toFixed(2));
    });

And now I want to mix that cryptChange function with AJAX call to pass parameter for callback and use that in $ ('#tt-11').html (btc2 here <==.PLN.toFixed (2);

Is that clearer guys now?

4
  • 1
    I'm not sure I understand what your asking for with your last sentence. Edit: I'm also trying to pull up the api, because you shouldn't have to construct your url that way. getJSON should be able to accept an object with key value pairs and it will build the query string on the url for you. Commented Sep 14, 2018 at 21:12
  • 1
    api.jquery.com/jQuery.getJSON/#jQuery-getJSON-url-data-success yeah, so if you look at the api, the second optional argument you can give it is data. So you could have a constant variable some where with https://min-api.cryptocompare.com/data/dayAvg in it, use that variable for your url, and then give data as {fsym:'BTC', sym:'PLN'} or whatever, and getJSON will construct the url for you. This is a side note to whatever you as asking for at the bottom of your question. Commented Sep 14, 2018 at 21:15
  • 1
    It is not a good habit to delete the previous question where we already tried to figure out what you are asking and create a new question with the exact same content. Commented Sep 15, 2018 at 0:06
  • t.niese i know, but there was a mess, so here i just did more accurate topic, thank you for all your help ;) i appreciate that helping :) Commented Sep 15, 2018 at 17:28

1 Answer 1

2

Define a function that takes all the varying parts as parameters.

function getCrypt(from, to, id) {
    $.getJSON('https://min-api.cryptocompare.com/data/dayAvg', {
        fsym: from,
        tsym: to
    }, function(result){
    $('#' + id).html(result[to].toFixed(2));
});

You can then do:

getCrypt('BTC', 'PLN', 'tt-11');
getCrypt('BCH', 'PLN', 'tt-12');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You very much, it's working perfectly well, this is what i was looking for, but i couldn't get into it :) i just didnt know that the function params, that object i dont need to specify as parameter, but it's just data collected from call ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.