0

i am new to javascript and am trying to go through tutorials to understand the concepts.

this is my function

function toCelsius(fahrenheit){

    (5/9) * (fahrenheit-32);    
    document.getElementById("demoTemp").onclick.toString();

}

and i am trying to invoke it here:

<form>
<br>
Enter number: <input type="text" name="firstname">

<!-- <input type="text" name="lastname"> -->
<button type="button" onclick="toCelsius()"> new temp in Celsius </button>
</form>

<p id="demoTemp"></p>

i want the user to be able to enter a value and then hit the button that will tell them their new temperature from fahrenheit to celsius

1
  • Elegant way is first attach a 'click' event listener to required button. [developer.mozilla.org/en-US/docs/Web/API/… Then perform your logic as per one of the following answers. It is important to understand event binding first. Also explore that event object. Commented Nov 27, 2014 at 10:56

3 Answers 3

4

Just put the result in the inner html.

function toCelsius(){
    var fahrenheit = document.getElementsByName("firstname")[0].value;
    var cel = (5 / 9) * (fahrenheit - 32);    
    document.getElementById("demoTemp").innerHTML = cel;
}
Sign up to request clarification or add additional context in comments.

1 Comment

He dont want pass fahrenheit, but get input value, use document.getElementById("id").val and add id id at your input
0

You should make your function in this way :

  function toCelsius(){    var fahrenheit = document.getElementById('firstname').value; var cel=     (5/9) * (fahrenheit -32);        document.getElementById("demoTemp").innerHTML = cel;}

Comments

0

You do not need to pass anything in the function. Try this JS :-

function toCelsius(){
    var fahrenheit = document.getElementsByName("firstname")[0].value;
    var cel = (5 / 9) * (fahrenheit - 32);    
    document.getElementById("demoTemp").innerHTML = cel;
}

Comments

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.