How to put degree character in html input field via javascript

I have a textbox like

Html

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

      

which works great as it displays the power sign fine, but when I try to set the value via jquery like

Jquery

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

Displays all text instead of the degree symbol.

+3


source to share


6 answers


You can target the attribute value

:

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

      



also better used .text()

for setting text because it is .val()

used to retrieve the value of an element

+11


source


You can use HEXCode like

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

      



Also go through this link

+2


source


$("#test").click(function(){ 
    $("#lat").val("29" + ascii(176))
});

function ascii (a) { return String.fromCharCode(a); }

      

Updated JSFiddle for further help: http://jsfiddle.net/zkh2of12/1/

+1


source


Use a special tag besides the input field, it will be clear to the user:

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

      

0


source


How can I simply set a symbol directly in JavaScript instead of HTML code?

$(function(){
  $("#set").click(function(){
    $("#lat").val("°");
  });
});
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

<button id="set">set</button>
      

Run code


If you want to improve, just create the hidden <span>

html code you like inside and then just select the html using JavaScript. This will work even for symbols that you cannot post to JS

0


source


Try the following:

$("#lat").attr("val","9"+ascii(176));

      

0


source







All Articles