Gaby Gaby - 4 days ago 5
jQuery Question

Change a Fahrenheit character into celcius character

enter image description here

I want to achieve this, When I click the Fahrenheit character,It will change into Celcius character.But not work, How to make it?

<html lang="en">




$("a").click(function() {
if ($(this).text() == "&#8457;") {
$(this).html("&#8451;");
} else {
$(this).html("&#8457;");
}
});

<script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css">
<link rel="stylesheet" href="css/owfont-regular.css">
<a id="toggle" href="javascript:void(0)">&#8457;</a>




Answer

This works for me

$("a").click(function() {
  if ($(this).text() == "℉") {
    $(this).text("℃");
  } else {
    $(this).text("℉");
  }
});
<script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<a id="toggle" href="javascript:void(0)">℉</a>

So does this

$("a").click(function() {
  var temp = $(this).text();
  $(this).text(temp == "℉"?"℃":"℉");
});
<script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<a id="toggle" href="javascript:void(0)">℉</a>

Comments