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°">
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°")
Displays all text instead of the degree symbol.
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
You can use HEXCode like
<a href id="my">Here</a>
<script>
$('#my').click(function()
{
alert('9\xB0');
});
</script>
Also go through this link
$("#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/
Use a special tag besides the input field, it will be clear to the user:
<input type="text" id="lat" placeholder="Latitude °" value=""> °
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°">
<button id="set">set</button>
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
Try the following:
$("#lat").attr("val","9"+ascii(176));