Call Javascript function on element onclick event


I need to call a javascript function on <a> element click event. When the page will initially load it will display the temperature in °C. Later on user can click on temperature to change it from °C to °F and vice versa without page refresh. Thanks for help..!!

Code :


<script src=""></script>

  <div class="mainContainer">
    <div class="tempOutCont">
      <div class="tempInCont">
        <span id="headDetails"></span>
        <div id="iconTemp">
          <img id="icon"></img>
          <a id="unitLink" href=""></a>
        <span id="mainWeather"></span>
        <span id="detailsHead">Details</span>
        <div id="listDetails">
            <li>Wind<span id="windText" class="listDetailsItem"></span></li>
            <li>Pressure<span id="pressureText" class="listDetailsItem"></span></li>
            <li>Humidity<span id="humidText" class="listDetailsItem"></span></li>
            <li>Cloudiness<span id="cloudText" class="listDetailsItem"></span></li>



body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
.mainContainer {
  background-color: red;
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
.tempOutCont {
  background-color: white;
  height: 20em;
  width: 40em;
  border-radius: 1em;
  margin-bottom: 10em;
  display: flex;
  justify-content: center;
  align-items: center;
.tempInCont {
  background-color: blue;
  margin: 2em;
  width: 100%;
  height: 80%;
  border-radius: 1em;
  display: flex;
  flex-direction: column;
#headDetails {
  border: 0.1em white solid;
  text-align: left;
  margin-top: 1em;
#iconTemp {
  border: 0.1em white solid;
  display: flex;
  justify-content: space-around;
#mainWeather {
  text-align: center;
#detailsHead {
  text-align: center;
#listDetails {
  text-align: center;
li {
  margin-left: 10em;
  text-align: left;
.listDetailsItem {
  margin-left: 5em;
  text-align: left;
a {
  color: white;


/*<iframe src="" allow="geolocation"></iframe>*/
var appId = "675645ead57721be136750f9cfc0acca";
var unit;
var temperature;

var changeUnit = function changeUnit(unit, temperature) {
  if (unit === "C") {
    temperature = temperature * 1.8 + 32;
    unit = "F";
    document.getElementById("unitLink").innerHTML = temperature + "°F";
  } else if (unit === "F") {
    temperature = (temperature - 32) / 1.8;
    unit = "C";
    document.getElementById("unitLink").innerHTML = temperature + "°C";
  } else if (unit === "K") {
    temperature = temperature - 273.15;
    unit = "C";
    document.getElementById("unitLink").innerHTML = temperature + "°C";
  return false;

var getWeatherData = function getWeatherData() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function showPosition(position) {
      var lat = String(position.coords.latitude);
      var lon = String(position.coords.longitude);
        type: "GET",
          "" +
          lat +
          "&lon=" +
          lon +
          "&appid=" +
        success: function(json) {
          document.getElementById("headDetails").innerHTML =
            "Weather in " + + "," +;
          document.getElementById("icon").src =
            "" +[0].icon + ".png";
          changeUnit("K", json.main.temp);
          document.getElementById("mainWeather").innerHTML =
          document.getElementById("windText").innerHTML =
            json.wind.speed + "m/s";
          document.getElementById("pressureText").innerHTML =
            json.main.pressure + "hpa";
          document.getElementById("humidText").innerHTML =
            json.main.humidity + "%";
          document.getElementById("cloudText").innerHTML =
            json.clouds.all + "%";
  } else {
    x.innerHTML = "Geolocation is not supported by this browser.";
$(document).ready(function() {
  $("#unitLink").onclick = changeUnit(unit, temperature);


What exactly is the problem you are having?
When i click on the text 17°C which is a anchor text i set using function call, nothing happens. The function call sets the text correctly by converting temperature from Kelvin(K) to Celcius(C). But after that nothing happens on <a> click.
– Shantanu Tomar
18 mins ago
This line $("#unitLink").onclick = changeUnit(unit, temperature); doesn’t do what you think it does. It just adds a custom property to a jQuery object.
So what i want to to do here is call a function on click event. May be you can suggest a better way to do that. Thanks
– Shantanu Tomar
17 mins ago
Please take a look at the basics here. $("#unitLink") returns a jQuery object, then you’re adding onclick property to that object, which eventually evaluates to false since changeUnit returns false. Or fails alltogether, if unit and/or temperature are not defined at the DOM ready.



If the questions is what I think it is, then you should try something like this

<a onclick='myFunction()'>Click Me</a>


Thanks for the answer. My problem here is that the function has arguments passed into it. How can i pass value from JS to HTML in that case ? Won’t be possible i guess.
– Shantanu Tomar
16 mins ago
Don’t bind obtrusively into HTML. This is a bad practice.
Are you trying to edit HTML elements with values set by your JS?
Yes.. I need to do that
– Shantanu Tomar
1 min ago


The propper way to bind events to DOM is:

<a href="#" id="someLink">link</a>

with JS:

// do something

Not using the onclick in HTML.

The advantages are

  • behaviour (Javascript) is separated from presentation (HTML)
  • no mixing of languages
  • you’re using a javascript framework like jQuery that can handle most cross-browser issues for you
  • You can add behaviour to a lot of HTML elements at once without code duplication