lundi 14 décembre 2015

Display another text when CheckBox is (un)checked

It should be quite simple. You have the following text:

Your scream: Whoops!

And when you check the checkbox below that says

Scream "yay" instead!

the word “Whoops!” should change to “Yay!”:

Your scream: Yay!

So my body is (I want to do it with a function):

Your scream: <script>GiveMeSomeScream()</script>!<hr>
<input type="checkbox" name="whatever" id="ScreamYay"> Scream "yay" instead!

But I’m having problems with the actual script. Here’s what I tried first:

function GiveMeSomeScream()
{  if (document.GetElementById('ScreamYay').checked == "true")
{  document.write("Yay");
}  else
{  document.write("Whoops");
}}

But it just wouldn’t work. So I expanded it like this:

var yay = document.getElementById('ScreamYay').checked
function GiveMeSomeScream()
{  if (yay == "true")
{  document.write("Yay");
}  else
{  document.write("Whoops");
}}

Now it gives “Whoops” every time (even if I make the checkbox checked defaultly), and that’s pretty much it.

I know it’s ridiculous I can’t even handle a checkbox, but still I would be glad for any help.




Aucun commentaire:

Enregistrer un commentaire