0

I'm trying to build a currency converter for a gaming marketplace & I currently have it so that the appropriate icon for each currency/payment method appears within the input bar - It has a default option for the payment option, but the span is blank until the user clicks a country. How do I set a default so that an icon appears 100% of the time?

I managed to make it work with the payment method icon, but i'm missing something with the country icon

https://codepen.io/anon/pen/byXrWN

    const data = [
    {
        currency: 'paypal',
        we_buy: 0.50,
        we_sell: 0.68,
        img_path: 'img/paypal.svg',
        icon: 'fab fa-paypal'
    },
    {
        currency: 'debit',
        we_buy: 0.67,
        we_sell: 0.82,
        img_path: 'img/debit-card.svg',
        icon: 'far fa-credit-card'
    },
    {
        currency: 'btc',
        we_buy: 0.58,
        we_sell: 0.77,
        img_path: 'img/bitcoin.svg',
        icon: 'fab fa-btc'
    },
    {
        currency: 'ethereum',
        we_buy: 0.59,
        we_sell: 0.76,
        img_path: 'img/ethereum.svg',
        icon: 'fab fa-ethereum'
    }
];

const country = [
    {
        country_id: 'USA',
        country_currency: 'USD',
        img_path: 'img/united-states.svg',
        icon: 'fas fa-dollar-sign',
        rate: 1.0
    },
    {
        country_id: 'EUR',
        country_currency: 'EUR',
        img_path: 'img/european-union.svg',
        icon: 'fas fa-euro-sign',
        rate: 0.88
    },
    {
        country_id: 'UK',
        country_currency: 'GBP',
        img_path: 'img/united-kingdom.svg',
        icon: 'fas fa-pound-sign',
        rate: 0.78
    },  
    {
        country_id: 'CAN',
        country_currency: 'CAD',
        img_path: 'img/canada.svg',
        icon: 'fas fa-dollar-sign',
        rate: 1.33
    }
];

const countryContainer = document.getElementById("countries");
let selectedCountry = null;
var selectCountry = function (index) {
    const cdata = country[index];
    selectedCountry = country[index];
    document.getElementById("country-selected").innerHTML = `Country selected: ${cdata.country_currency}`;
    document.getElementById("country_icon").className = cdata.icon;
};

// Image container

const imagesContainer = document.getElementById("currencies");
let selectedCurrency = null;
var selectCurrency = function (index) {
    const element = data[index];
    selectedCurrency = data[index];
    document.getElementById("currency-selected").innerHTML = `Currency selected: ${element.currency}`;
    document.getElementById("data_icon").className = element.icon; 
};

// made function originally in amount.onkeyup have a greater scope
const calculate = () => {
    const output_we_sell = document.getElementById("output_we_sell");
    if (amount.value === '') {
        output_we_sell.innerHTML = 0;
        return;
    }
    if (!isNaN(amount.value)) {
        output_we_sell.innerHTML = `${(+amount.value * selectedCurrency.we_sell).toFixed(2)}`;
    }
};
// ...
var selectCurrency = function (index) {
    const element = data[index];
    selectedCurrency = data[index];
    document.getElementById("currency-selected").innerHTML = `Currency selected: ${element.currency}`;
    document.getElementById("data_icon").className = element.icon;
    calculate(); // Added calculate here
};

var selectCountry = function (index) {
    const cdata = country[index];
    selectedCountry = country[index];
    document.getElementById("country-selected").innerHTML = `Currency selected: ${cdata.country}`;
    document.getElementById("country_icon").className = cdata.icon;
    calculate();
};

// ...
(() => {
    for (let i = 0; i < data.length; i++) {
        imagesContainer.innerHTML += `<div class="currency" onclick=selectCurrency(${i})><img id=${i} src=${data[i].img_path}></div>`;
    }
    selectCurrency(0);
    const amount = document.getElementById("amount");
    amount.onkeyup = calculate; // Changed this to use the calculate function{
    for (let i = 0; i < country.length; i++) {
        countryContainer.innerHTML += `<div class="country" onclick=selectCountry(${i})><img id=${i} src=${country[i].img_path}></div>`; 
    }
}

)();

The top row is meant to be the country selection & the bottom row is the payment method (poorly named in the html/js - currency = payment method)

an icon for the payment method should appear as the page loads, rather than after the user has clicked on their country of origin

2
  • 1
    Using boolean operator : data[i].img_path || 'defaultValue' Commented Jun 8, 2019 at 12:03
  • 1
    Note you set the currency to a default so do the same operation but for the country and before creating the html Commented Jun 8, 2019 at 12:11

1 Answer 1

1

I went through your code and found you are not calling a function to set up a default country. Just add selectCountry(0); to line 117 and it should work.

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

1 Comment

You absolute star! I can't believe it was something so simple! Thank you!

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.