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

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?

You could use…

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

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?

2 Likes

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

2 Likes

I just tested it. It worked perfectly. Thanks!