Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Printing result of a conversion to a textbox in JavaScript & HTML

So I'm trying to learn some JavaScript and currently I want to print a value of user input then print it to a textbox that I have.

In the console it works but it doesnt print anything to my gui.

code

function converter() {
  result = document.getElementById("converted");
  var to_convert = document.getElementById("to_convert").value;
  var unit = document.getElementById("unit").value;
  var converted = 0;

  //Fahrenheit till celsius
  if (unit = "F") {
    var converted = 1.8 / to_convert;
  }

  // Cup till liter
  if (unit = "cup") {
    var converted = 0.236 * to_convert;
  }

  //lb till kg
  if (unit = "lb") {
    var converted = 0.454 * to_convert;
  }
  result.textContent.value = converted;
  console.log(converted);


  // (F - 32) / 1.8 = Celsius
  // cup * 0.236 = liter
  // lb * 0.454 = kg
}
<form>
<fieldset>
<input type="text" id="to_convert" value="" size="5" /><br/>
    <select id="unit">
      <option value="F">Fahrenheit to Celcius</option>
      <option value="cup">cup to liters</option>
      <option value="lb">lb/pound to kg</option>
    </select>
    <input onclick="converter()" value="Konvertera" type="button" />
    <input type="text" id="converted" value="[resultat här]" size="15" />
  </fieldset>
</form>

code

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This is wrong! : result.textContent.value = converted
The right one is : result.value = converted

function converter() {
  result = document.getElementById("converted");
  var to_convert = document.getElementById("to_convert").value;
  var unit = document.getElementById("unit").value;
  var converted = 0;

  //Fahrenheit till celsius
  if (unit = "F") {
    var converted = 1.8 / to_convert;
  }

  // Cup till liter
  if (unit = "cup") {
    var converted = 0.236 * to_convert;
  }

  //lb till kg
  if (unit = "lb") {
    var converted = 0.454 * to_convert;
  }
  result.value = converted;
  console.log(converted);


  // (F - 32) / 1.8 = Celsius
  // cup * 0.236 = liter
  // lb * 0.454 = kg
}
<form>
<fieldset>
<input type="text" id="to_convert" value="" size="5" /><br/>
    <select id="unit">
      <option value="F">Fahrenheit to Celcius</option>
      <option value="cup">cup to liters</option>
      <option value="lb">lb/pound to kg</option>
    </select>
    <input onclick="converter()" value="Konvertera" type="button" />
    <input type="text" id="converted" value="[resultat här]" size="15" />
  </fieldset>
</form>

about 4 years ago · Juan Pablo Isaza Report

0

You need to assign it like this: result.value = converted

result.value = converted
about 4 years ago · Juan Pablo Isaza Report

0

You need to replace result.textContent.value = converted by result.value = converted

 result.value = converted;

And It will work fine.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!