Saturday, December 17, 2005

Javascript fun

Today I had a real nice experience with javascript. I wanted to write some code, had half an idea on how it could be done, and found, thanks to, as always, Google, the other part.

In my blog think again! I wanted to show 19 questions and answers in the game Twenty Questions. It would spoil the fun to display all 19 questions at once, so I started to make them invisible:



Everything in the div block "news1" will not be shown thanks to its style.

To display it at a click of a button, I need, suprise! surprise!, a button:



When the button is clicked, it calls the function jfx() with the value "true".

This is the function:

function jfx(status){
if (status)
{document.getElementById("news1").style.display = "block"};
}


To avoid using nineteen buttons, I used a variable:


var counter=1;

function jfx(status){
var b = "news"+counter;
if (status)
{document.getElementById(b).style.display = "block"};
if (counter == 19)
{document.getElementById("button").style.display = "none"};
counter++;
}


By putting the button itself in a div block, I can hide it when the 19th question have been shown.

To see the code in action, click here.

No comments: