First of all, I have to change the value of the input type radio signal to true or false. Also, I think you are not writing the code you want. For example, at first both divs are hidden, and nothing makes them visible (because the radio is also hidden).
I think this is definitely the best approach to this:
<!DOCTYPE html> <html lang = "en"> <head> <meta charset="utf-8"> <title>forms.html</title> <h1> Welcome </h1> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jqueryui. css"> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> <script> $(document).ready(function(){ $("img") .mouseover(function() { $(this).attr("src", "pika2.jpg"); }); $("img").mouseout(function() { $(this).attr("src", "pika1.jpg"); }); $("#whyNot").hide(); $(':radio[name="list"]').change(function() { var like = $(this).filter(':checked').val(); if (!JSON.parse(like)) { $("#whyNot").show(); } else { $("#whyNot").hide(); } }); }); </script> </head> <body> <img src="pika1.jpg" alt="piakchu"/> <p> Please tell me who you are: </P> <form action="sumbit.html"> <p> <label for="First Name">First name:</label> <input type = "text" name="First Name" id="First Name" autofocus> </P> <div> <label for="Last Name">Last Name:</label> <input type = "text" name="Last Name" id="Last Name" required > </div> <div> <p> Do you like computer science: </p> <p><input type="radio" name="list" value="true"/> <label for="yes">Yes</label> <input type="radio" name="list" value="false"/> <label for="No">No</label> </P> </div> <div id="whyNot"> <p> Why don't you like computer science?: </p> <textarea name="textarea" rows="5" cols="30" placeholder="Enter your text here (optional)"> </textarea> </div> <div> <input type="submit" value="Submit your answers"/> </div> </form> </body>
source share