Currency Converter

Hey Guys

Im trying to finish this project on JS and im having problems with this error:
Uncaught TypeError: Cannot read property ‘value’ of null

it appears here:

function converter(moedaEscolhida) {
  var conversao = document.getElementById('conversao')
  var inputValor = document.getElementById('inputValor').value

Uncaught TypeError: Cannot read property ‘value’ of null

var dolar = 5.51
var libra = 7.60
var euro = 6.54
var moedaEscolhida = ""

function escolhaConversao () {
  var option = document.getElementById("select").value
  if(option != "") {
    if(option != "dolar") {
      valor.innerHTML = '<label for="inputValor">Informe o valor em Dólares($)</label>' 
      moedaEscolhida = 'dolar'
    } else if (option == "libra") {
      valor.innerHTML = '<label for="inputValor">Informe o valor em Libras</label>'
      moedaEscolhida = 'libra'
    } else {
      valor.innerHTML = '<label for="inputValor">Informe o valor em Euros</label>'
      moedaEscolhida = 'euro'
    }
    valor.innerHTML += '<br />'
    valor.innerHMTL += '<input type="number" id="inputValor" class="inputValor"/>'
    valor.innerHTML += '<br />'
    valor.innerHTML += '<button id="converter" onclick="converter(moedaEscolhida)">Converter</button>'    
  }
}

function converter(moedaEscolhida) {
  var conversao = document.getElementById('conversao')
  var inputValor = document.getElementById('inputValor').value
  if (moedaEscolhida == 'dolar') {
    var dolarParaReal = inputValor * dolar
    conversao.innerHTML = '<label class="valorConvertido">R$ '+inputValor+' = $ '+dolarParaReal.tofixed(2)+'</label>'
  } else if (moedaEscolhida == 'libra') {
    var libraParaReal = inputValor * libra
    conversao.innerHTML = '<label class="valorConvertido">R$ '+inputValor+' = £'+libraParaReal.tofixed(2)+'</label>'
  } else {
    var euroParaReal = inputValor * euro
    conversao.innerHTML = '<label class="valorConvertido">R$ '+inputValor+' = €'+euroParaReal.tofixed(2)+'</label>'
  }
}

Thats my code:

https://codepen.io/jqm00/full/XWprOVb

var inputValor = document.getElementById('inputValor').value

The above line refers to an element with an id of inputValor. I do not see such an element in your HTML.

1 Like

Thanks for your support, man!

I took a code from other friend like an example and that is working but mine is note.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.