Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a degree symbol in an html input field through javascript

I have a text box as

HTML

<input type="text" id="lat" placeholder="Latitude" ng-lat value="9&#176;"> 

which works fine as it displays the degree sign perfectly, but when I try to set the value through jquery like

Jquery

$("#lat").val("9&#176;")

Its displaying the entire text instead of the degree symbol.

like image 973
BiJ Avatar asked Jul 28 '15 07:07

BiJ


People also ask

How do I add a degree symbol in JavaScript?

Unicode Characters: To print the degrees symbol in JavaScript, we need to use the write \u00B0 to represent the unicode character for the degress symbol. Store a celsius temperature into a variable.

How do you input JavaScript in HTML?

You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.


2 Answers

You can target the value attribute:

$("#lat").attr('value',"101"+String.fromCharCode(176))

it is also better to use .text() to set the text because .val() is used to retrieve the value of an element

like image 159
Pindo Avatar answered Sep 23 '22 01:09

Pindo


You can use HEXCode, e.g

 <a href id="my">Here</a> 
<script>
    $('#my').click(function()
                    {
    alert('9\xB0');
    });
    </script>

Also go through this link

like image 21
Parag Jadhav Avatar answered Sep 22 '22 01:09

Parag Jadhav