How to Display a JavaScript Variable in Hype Text Box (Element)?

(Andrew) #1

I am creating a quiz widget for an iBook. I would like to display the problem number, and the current score on the top of the page. I currently have created two text elements but these have only dummy text. I would like to replace the numbers in the dummy text with values returned from my JavaScript functions. Is this possible?

(Greg) #2

You could use…

hypeDocument.getElementById('MyElement_id').innerHTML = MyVariable;

(Andrew) #3

Thanks for the advice! Let me see if I follow correctly…

In my case I could select the desired text element, and via the Identity inspector set the Unique Element ID to (as an example) currentScoreText. Then, in my JavaScript function I include the lines:

var right = 3;
var wrong = 2;
hypeDocument.getElementById(‘currentScoreText’).innerHTML = “Current Score: " + right + " of " + (wrong + right) + " correct”;

The output in the textbox should be: Current Score: 3 of 5 correct

Should that work?

(Greg) #4

Perfecto! it will return “Current Score: 3 of 5 correct”

(Andrew) #5

I just tested it. It worked perfectly. Thanks!