I get Nan when I try to pass a number to .innerHTML


#1

I’m struggling to get the numeric content of a variable to hypeDocument.getElementById(id).innerHTML…
If I pass a letter it works, but a number results in a NaN…I know it must be something simple I’m doing wrong but my limited javascript knowledge is holding me back.

Can someone please help me on my way?
I’m just trying to increment a number on the display of a UI demo…

thanks


(Mark Hunte) #2

This should be simple to fix but can you post the code you are using and or a example project…


#3

Here is the code that I’m calling from a scene-keypress-action (‘level’ is the ID of the 2 digit number I want to increase). Ultimately I’d like to use the arrow keys to both increase and decrease as well as have it rund faster the longer i depress the key…but first I just want to work.

Thanks
anders

var l = hypeDocument.getElementById('level').innerHTML;

l = Number(l) + 1;

hypeDocument.getElementById('level').innerHTML = l;

(Mark Hunte) #4

Sorry, Can you post an example project so I can see what you are actually using.

The code is pretty basic and not showing what we need to see…


#5

You want to make sure l is a number, so use:

var l = parseInt(hypeDocument.getElementById('level').innerHTML);


(Mark Hunte) #6

Number() will pretty much do the same thing AFAIK.

I suspect that @and has formatting in the innerHTML but wanted to see first so as not to keep guessing.

I quick fix for that is to use innerText instead of innerHTML.


#7

Couldn’t get it to work and changed it to a switch with different actions on the arrow keys and suddenly it worked… Wierdly

var x = hypeDocument.getElementById('level').innerHTML;

switch(event.keyCode){ 
case 38:
x++;
break;

case 40:
x = x-1;
break;

case 39:
hypeDocument.continueTimelineNamed('Main Timeline', hypeDocument.kDirectionForward, false);
break;

case 37:
hypeDocument.goToTimeInTimelineNamed(1, 'Main Timeline');
hypeDocument.continueTimelineNamed('Main Timeline', hypeDocument.kDirectionForward, false);
break;
}

hypeDocument.getElementById('level').innerHTML = x;