Javascript object methods

This is my situation
//The object

var obj = { celToFah: function() {
  var celsiusInn = document.getElementById('celsius-input').value;
  var fahrenheit;
   fahrenheit = (celsiusInn * 9/5) + 32
   document.getElementById('result1').innerHTML = fahrenheit;
},
 fahToCel: function() {
  var fahrenheitInn = document.getElementById('fahrenheit-input').value;
  var celsius;
  celsius = (fahrenheitInn - 32) * (5/9) 
  document.getElementById('result1').innerHTML = celsius;
}
}

//The action

<button onclick="obj.fahToCel()" class="btn btn-primary"> Calc</button>

this code only works when the script in the html page but doesnt work when refrenced from a js file.
please help

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Ok buh do you have any help for my problem ?

If you have your JS code in a separate file, you can’t add an event listener to your button like you’re currently doing. You have to add it within your script file instead:

document.querySelector('button').addEventListener('click', obj.fahToCel)

Need to see more of the code (ie the htmlso can see where in the html you’re referencing the script).

Also the actual error would be helpful: open the browser console and see what the error is when you click that element.

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