Home > OS >  javascript convert dollars to egyptian pound
javascript convert dollars to egyptian pound

Time:10-11

I have this form when the user put a number in dollar and i want to convert this number to egyptian pound

I want to use javascript to do that

let dollarInput = document.querySelector("[name='dollar']");
let result = document.querySelector(".result");


dollarInput.addEventListener("input", () =>{
  if(dollarInput.value !== "" && dollarInput > 0){
     let eG = dollarInput.value * 15.6;
     result.textContent =`{${dollarInput.value}} USD Dollar = {${eG.toFixed(2)}} Egyptian Pound`;
  } 
});
<form action="">
  <input type="number" name="dollar" placeholder="USD Dollar" />
  <div class="result">{0} USD Dollar = {0} Egyptian Pound</div>
</form>

I'm using addEventListener when i put a number to make a change in the div i want to print the value in dollar than en egyptian pound

but my code it's not working can anybody help me to know why and how should i do it

CodePudding user response:

You should be checking whether the value of the input is greater than 0, not the input element itself.

let dollarInput = document.querySelector("[name='dollar']");
let result = document.querySelector(".result");


dollarInput.addEventListener("input", () =>{
  if(dollarInput.value !== "" && dollarInput.value > 0){
     let eG = dollarInput.value * 15.6;
     result.textContent =`{${dollarInput.value}} USD Dollar = {${eG.toFixed(2)}} Egyptian Pound`;
  } 
});
<form action="">
  <input type="number" name="dollar" placeholder="USD Dollar" />
  <div class="result">{0} USD Dollar = {0} Egyptian Pound</div>
</form>

  • Related