Asfourhundred
Asfourhundred

Reputation: 3363

Print result of JS file in html

So I have an html file with the following code:

<script type="text/javascript">
    document.write(getHighestCacheMan());
</script>

And I have this Javascript code in a .js file with the code:

function getHighestCacheMan()
{
    var cache = currentCaches[0];
    for(var i = 1; i < currentCaches.length; i++) {
    if(currentCaches[i].altitude != -32768)
        if(currentCaches[i].altitude > cache.altitude)
        cache = currentCaches[i];
    }
    return cache.name;
}

The files are correctly linked together and everything else that is intertwined between files works as intended.

It doesn't show anything. Is there any other way to do this ?

Upvotes: 0

Views: 4414

Answers (3)

Asfourhundred
Asfourhundred

Reputation: 3363

As said by a lot of people the answer was indeed to use innerHTML.

As so I now have in my .html:

<br />Name: <SPAN id='cacheName'></SPAN>

and on my .js:

document.getElementById('cacheName').innerHTML = highestCache.name;

And it now works as intended! Thank you to everybody who helped.

Upvotes: 0

xsami
xsami

Reputation: 1330

Try using innerHTML:

var currentCaches = [ {altitude: 12, name: "Jhon"}, {altitude: 23, name: "peter"}, {altitude: 10, name: "francisco"}, {altitude: 590, name: "Eddy"}];
function getHighestCacheMan()
{
    var cache = currentCaches[0];
    for(var i = 1; i < currentCaches.length; i++) {
    if(currentCaches[i].altitude != -32768)
        if(currentCaches[i].altitude > cache.altitude)
            cache = currentCaches[i];
    }
    return cache.name;
}

document.getElementById("HighestCacheMan").innerHTML = getHighestCacheMan();
<div id="HighestCacheMan"></div>

Upvotes: 1

Sagar Roy
Sagar Roy

Reputation: 443

You should write a id="demo"

<script type="text/javascript">
   document.getElementById("demo").innerHTML=getHighestCacheMan();
</script>

Upvotes: 1

Related Questions