user2669997 user2669997 - 2 months ago 7
Ajax Question

attach string to json ajax result

I have an ajax script that returns the temperature of a probe. I would like to add deg C tot he result but do not know how to do this. Can some one help. My script is below



<script>
setInterval(function(){
$.ajax({
url:"data.php",
dataType:"json",
success: function(data){;
$('#niamh').html(data.Gniamh);
}
});
}, 1000);
</script>





This return the temp as a value and places it in the Div tags but I would like deg added to it. I have tried putting °C with in the Div tags but this gets written over.

Answer

you can either add it to the return function:

<script>
 setInterval(function(){
     $.ajax({
         url:"data.php",
         dataType:"json",
         success: function(data){;
            $('#niamh').html(data.Gniamh + "&deg;C");
         }
     });
 }, 1000);

or wrap the result div with a parent:

<div><div id="niamh"></div>&deg;C</div>