1

This posting replaces my original challenge with a correctly working example. Radio buttons - which answer is right?

I wanted to use only html and javaScript - that is where I am mostly comfortable at this stage of my education

My challenge WAS that

 - I had images within an html table
 - I wanted to display an image of a light bulb
       after the user clicked the correct radio button
       and clicked on a submit form type button (onSubmit)

I could not get the light bulb pictures to display after a correct choice

I could get the light bulb image to display outside of my onSubmit function

I initially set these light bulb images to not be visible, within the html displaying the table

I noted that, sometimes after a correct answer was chosen, the light bulb image would flash, then become non-visible again. I then searched around and saw that, indeed, the onSubmit process resets values to take on their original attributes.

Not being sophisticated, yet, with jQuery, I was unable to get the suggestions made by others to work. I finally realized I did not have to use an onSubmit type button at all (thought one of the folks who answered this suggested something along those lines, but cannot find it now - if I find it I will vote up their answer)

Note 1: I have embedded the javaScript within the html only for purposes of this example. I do keep my functions in separate files. (re: comment by one of suggestions)

Note 2: This example does not clear the radio button setting after use. That is quite do'able and there is a good example of this, which works but is not included. You can find that at How to clear radio button in Javascript?

Below is the example code that does what I was originally trying to do. Notice - I'm not using any type of form or form submit function, as that gave me problems. So the suggestions made by others, might be very helpful for someone who still needs to use forms/form checking. first answer is wrong - no light picture should be displayed second answer is right - a light bulb picture is displayed

<html>
<head>
    <title></title>
</head>
<body>
    <center>
        What is this bird called?
        <p><img alt="a bird" height="150" id="photo" src=
        "img/testImages/spotted%20Sandpipler.JPG" style="clear: left" width=
        "100"></p>
    </center>
    <button onclick="checkAnswer()">Check Answer</button>
    <p></p>
    <table>
        <tr>
            <td><input id='first' name="operation" type="radio" value="1">
            <label for="alsoFirst">Answer 1</label></td>
            <td><img height="52" id="light1" src="img/alternateCoolBulb.jpeg"
            style="visibility: hidden;" width="42"></td>
        </tr>
        <tr>
            <td><input id='second' name="operation" type="radio" value="2">
            <label for="alsoSecond">Answer 2</label></td>
            <td><img height="52" id="light2" src="img/alternateCoolBulb.jpeg"
            style="visibility: hidden;" width="42"></td>
        </tr>
    </table>
    <script type="text/javascript">


        //history

        //https://stackoverflow.com/questions/2554116/how-to-clear-radio-button-in-javascript

        //https://stackoverflow.com/questions/32292962/javascript-how-to-change-radio-button-label-text
        //https://stackoverflow.com/questions/37954245/image-will-not-switch-between-hidden-and-show

         document.addEventListener('readystatechange', function() {
          // Seems like a GOOD PRACTICE - keeps me from getting type error I was getting

            // https://stackoverflow.com/questions/14207922/javascript-error-null-is-not-an-object

            if (document.readyState === "complete") {
              init();
            }
          });

         function init() {

            var label = document.getElementsByTagName('label') [0]; 
            label.innerHTML = 'Curlew ';

             var label1 = document.getElementsByTagName('label') [1]; 
            label1.innerHTML = 'Sandpiper';


            }



    function checkAnswer() {
       var clickedIt = "yes";
           console.log ("did I get to myNEWFunction? ", clickedIt);

          if(document.getElementById('first').checked == true)
            {
            //alert ( "You have selected the first answer - WRONG" );  
            console.log( "You have selected the first answer - WRONG" );  

            }
          else
            if(document.getElementById('second').checked == true)
                {

                console.log( "You have selected the second answer - RIGHT" );  
                document.getElementById("light2").style.visibility = "visible";  

                }

    }

    </script> 
</body>
</html>
Community
  • 1
  • 1
LaurelS
  • 492
  • 2
  • 8
  • 22
  • In the future please help us help you by formatting your code better. – Jon P Oct 11 '16 at 23:55
  • On another side note, the `center` tag was depricated way back in HTML 4 : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/center – Jon P Oct 12 '16 at 00:03

3 Answers3

1

Instead of using

document.getElementById("light2").style.visibility = "visible";

you could use

document.getElementById("light2").style.display = "block"; 

and

img style="display: none;"
gitsitgo
  • 6,589
  • 3
  • 33
  • 45
  • So I changed the table cell to This light bulb image ("light2") is hidden when the image is first included in html tags – LaurelS Sep 28 '16 at 20:04
  • and I changed the line in the OnSubmitForm function to //document.getElementById("light2").style.visibility = "visible"; document.getElementById("light2").style.display = "block"; – LaurelS Sep 28 '16 at 20:05
  • But I still see the same behavior - no console errors and the light bulb is not displayed – LaurelS Sep 28 '16 at 20:06
  • Thank you for the suggestion. Maybe there is something else I should be doing with this idea. – LaurelS Sep 28 '16 at 20:15
  • going to update my example with your suggestions as I understand them, though I get the same behavior – LaurelS Sep 28 '16 at 21:10
0

You can use jQuery in order to perform your operation (and write cleaner code)

$('#light2').css('display','block');

This basically assigns display to block.

You can setup an onClick event for a button or call this when you click something.

$('#button').on('click', function(){
//Write your logic here.
$('#light2').css('display','block');
});
TechnoCorner
  • 4,879
  • 10
  • 43
  • 81
  • I'm trying to figure out where to try these pieces. I know I need to find the link to jQuery. Then I think I would put the $('#light2').css('display','block'); part just anywhere in the script section? Or do I need to somehow incorporate it in the html section? Then I assume I put the second set of lines into my form submission function. – LaurelS Sep 28 '16 at 20:17
  • Well my guess was not so good! I put into a jQuery link that I had for another example Then I put your first line into my init function - that ran clean with no console log errors. So then I added the line $('#light2').css('display','block'); into my submit function. All my window contents cleared, but the console.log s stayed! Clearly there are some things I don't know how to do yet. Maybe I have to make the jQuery into a separate function. – LaurelS Sep 28 '16 at 20:25
  • You can have a – TechnoCorner Sep 28 '16 at 20:33
  • I am only embedding the javascript in the example, because it is an example I synthesized down so I could communicate. I agree with you that it is much more desirable to have separate scripts. – LaurelS Sep 28 '16 at 21:04
  • But if I cannot get the example working with embedded javascript/jQuery I won't be able to move on to that clean code :-) – LaurelS Sep 28 '16 at 21:05
  • I think the image is flashing visible, just after the submit function finishes. – LaurelS Sep 29 '16 at 16:20
  • Have been experimenting more, and it seems that use of a form submit resets all values to original. I then fooled around with idea of adding the image via javascript, but that resets too.... I don't understand the examples I've turned up, that prevent resetting fields, though I've tried experimenting with those too. – LaurelS Sep 29 '16 at 17:42
  • I finally figured out how to fix my problem. I'm sure the above folks had good ideas - I just don't yet have the skills with what they suggested to understand how to incorporate their suggestions. I am about to edit my code to show what I did - Basically I quit using an onSubmit - which ties into one of the suggestions made, but I did it without using jQuery, which I'm only just starting to use. – LaurelS Oct 11 '16 at 23:19
0

As shown in my totally revised posting of my challenge and code, I resolved my problem by eliminating an onSubmit type function. As I worked with this problem I found that this type of function resets information in the html document (DOM - is this correct use of term?) to original values. I did not want that reset to occur, because I wanted to display an, initially hidden, image once the user chose a correct answer. I changed the code to now use a regular onClick type function, and now my images stayed displayed. It does mean I have to do other things myself, such as clearing radio buttons, and perhaps more.

LaurelS
  • 492
  • 2
  • 8
  • 22