JQuery is capable of hiding a div using the .hide() method but is unable to reveal it using the

I am currently working on a basic webpage that includes a question and a set of radio buttons. Depending on the option selected by the user, the question will be hidden and either a correct or incorrect message will be displayed, both of which are initially hidden.

The functionality is achieved using JQuery on the form submission event. The idea is to hide the question, determine which radio button was selected, and then display the corresponding message using the .show() method. However, the issue I am facing is that everything is hidden correctly, but the messages are not being displayed. I have tried debugging by changing the order of actions and found that it actually hides the elements again upon form submission. Any suggestions on what might be causing this behavior?

Below is the code snippet:

$(document).ready(function(){
    $('#passed').hide();
    $('#allGo').hide();
    $("#onlyPhilip").hide();
    $("#nobodyGoes").hide();


    $("#carBoatForm").submit(function(e){
      e.preventDefault();
      $("#question").hide();
      if($('#withoutMark').is(':checked')){
        $('#passed').show();
      } else if($('#theyAllGo').is(':checked')){
        $('allGo').show();
      } else if ($('#onlyPhilipGoes').is(':checked')) {
        $('#onlyPhilip').show();
      } else if ($('#nobodyCanGo').is(':checked')) {
        $('#nobodyGoes').show();
      }
    });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row" id="question">
      <div class="col-md-8 col-md-offset-3">
        <h2 class="challengeTitle">Challenge 2</h2>
        <h3 class="challengeHeading">The Big IF!</h3>
        <p class="challengeText">Philip and Susan are taking a trip by boat. The boat's rules are that a car's total weight must be below 2066kg.
        <br>Philip's car weighs 1865kg
        <br>Philip weighs 75kg
        <br>Susan weighs 50kg
        <br>Then their friend Mark asks if he can come along. Mark weighs 67kg.</p>
        <p class="challengeText">Which of the following statements are true?</p>

        <form id="carBoatForm">
          <input type="radio" name="boatAnswer" id="theyAllGo"> They can all go on the trip <br>
          <input type="radio" name="boatAnswer" id="onlyPhilipGoes"> Only Philip can go on the trip <br>
          <input type="radio" name="boatAnswer" id="withoutMark"> Philip and Susan can go, but Mark cannot <br>
          <input type="radio" name="boatAnswer" id="nobodyCanGo"> Nobody can go <br>
          <input type="submit" value="Enter">
        </form>
    </div><!--End Content Section -->

    <div class="row">
      <div class="col-md-8 col-md-offset-4">
        <div id="passed">
          <h2>Good Job!</h2>
          <h3>Philip and Susan can go on the trip, but sadly Mark would put the total weight over by 1kg, so he cannot come along.</h3>
          <button type="button" class="btn btn-primary"> Continue </button>
        </div>

        <div id="onlyPhilip">
          <h2> Close but nope!</h2>
          <h3>Although it is true that Philip could go on his own, the trip isn't restricted to only Philip being able to go.</h3>
          <button type="button" class="btn btn-primary"> Try Again </button>
        </div>

        <div id="allGo">
          <h2> Sorry this is wrong!</h2>
          <h3>Try again focusing on the weights. Can they all definitely go on? Remember the boat checks the total weight of a car.</h3>
          <button type="button" class="btn btn-primary"> Try Again </button>
        </div>

        <div id="nobodyGoes">
          <h2> Incorrect!</h2>
          <h3>Sure nobody has to go on the trip, but where's the fun in that! Try again, this time maybe let some people have fun.</h3>
          <button type="button" class="btn btn-primary"> Try Again </button>
        </div>
      </div>
    </div><!--End Section -->

Answer №1

You've concealed #question, the container for both questions and answers. Move id="question" to the row containing the questions. Additionally, ensure to include the # in $('#allGo').show();

$(document).ready(function(){
    $('#passed').hide();
    $('#allGo').hide();
    $("#onlyPhilip").hide();
    $("#nobodyGoes").hide();


    $("#carBoatForm").submit(function(e){
      e.preventDefault();
      $("#question").hide();
      if($('#withoutMark').is(':checked')){
        $('#passed').show();
      } else if($('#theyAllGo').is(':checked')){
        $('#allGo').show();
      } else if ($('#onlyPhilipGoes').is(':checked')) {
        $('#onlyPhilip').show();
      } else if ($('#nobodyCanGo').is(':checked')) {
        $('#nobodyGoes').show();
      }
    });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
      <div class="col-md-8 col-md-offset-3" id="question">
        <h2 class="challengeTitle">Challenge 2</h2>
        <h3 class="challengeHeading">The Big IF!</h3>
        <p class="challengeText">Philip and Susan are taking a trip by boat. The boat's rules are that a cars total weight must be below 2066kg.
        <br>Philip's car weighs 1865kg
        <br>Philip weighs 75kg
        <br>Susan weighs 50kg
        <br>Then their friend Mark asks if he can come along. Mark weights 67kg.</p>
        <p class="challengeText">Which of the following statements are true?</p>

        <form id="carBoatForm">
          <input type="radio" name="boatAnswer" id="theyAllGo"> They can all go on the trip <br>
          <input type="radio" name="boatAnswer" id="onlyPhilipGoes"> Only Philip can go on the trip <br>
          <input type="radio" name="boatAnswer" id="withoutMark"> Philip and Susan can go, but Mark cannot <br>
          <input type="radio" name="boatAnswer" id="nobodyCanGo"> Nobody can go <br>
          <input type="submit" value="Enter">
        </form>
    </div><!--End Content Section -->

    <div class="row">
      <div class="col-md-8 col-md-offset-4">
        <div id="passed">
          <h2>Good Job!</h2>
          <h3>Philip and Susan can go on the trip, but sadly Mark would put the total weight over by 1kg, so he cannot come along.</h3>
          <button type="button" class="btn btn-primary"> Continue </button>
        </div>

        <div id="onlyPhilip">
          <h2> Close but nope!</h2>
          <h3>Although it is true that Philip could go on his own, the trip isn't restricted to only Philip being able to go.</h3>
          <button type="button" class="btn btn-primary"> Try Again </button>
        </div>

        <div id="allGo">
          <h2> Sorry this is wrong!</h2>
          <h3>Try again focusing on the weights. Can they all definitely go on? Remember the boat checks the total weight of a car.</h3>
          <button type="button" class="btn btn-primary"> Try Again </button>
        </div>

        <div id="nobodyGoes">
          <h2> Incorrect!</h2>
          <h3>Sure nobody has to go on the trip, but where's the fun in that! Try again, this time maybe let some people have fun.</h3>
          <button type="button" class="btn btn-primary"> Try Again </button>
        </div>
      </div>
    </div><!--End Section -->

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with the count up functionality in jQuery

I'm currently working on a project involving countups. My aim is to have multiple countups displayed on a single page. While having one countup using interval() function poses no issues, I encounter trouble when trying to display two or more countups ...

What are some effective ways to utilize asynchronous ORMs without getting bogged down in overly long callback

Currently, I am utilizing the Joose Javascript ORM plugin, which is still quite young (project page). This is being used to store objects in an Appcelerator Titanium mobile project (company page). Due to the storage being on the client side, the applicatio ...

Does the XMLHttpRequests have a NavigationTiming interface that can be utilized?

While the window.performance object offers insights into the performance of the browser's last page load, such as DNS lookup times, I have not come across a similar feature for Ajax calls. The overarching issue I am aiming to address is the ability t ...

The process of retrieving multiple data using an ajax call upon selecting a specific option from a dropdown menu using jquery and php

I have been working on a project involving core PHP, jQuery, and AJAX. Currently, I am faced with a situation where I have a select box populated with data from a database. When a particular option in the select box is clicked, I need to retrieve data rela ...

What is the best way to incorporate template literals (` `) into existing template literals?

I am facing a unique challenge where I need to utilize a template literal within another template literal, but I am struggling to make it work. The code snippet in question looks like this: <p>Something something <a href={`${SOMELINK}/blah`}> ...

Looking for a checkbox change event that can detect and respond to changes in the checked state made programmatically

(jQuery 1.4.4) I am facing an issue with a checkbox where the .change() listener works well when the user manually clicks on the checkbox. However, I also require it to trigger when I programmatically change the status of the checkbox in jQuery using .at ...

Move the dropdown selection above all DIV elements

Check out this link: Make sure to select an option from the dropdown menu. Also, some of the options are hidden behind other elements. I noticed that if I target .join-form and remove overflow: hidden, the layout of the page becomes distorted. Can anyon ...

Converting timestamps: Retrieve day, date, hour, minutes, etc. without utilizing the "new Date()" function

Currently developing a web-app and faced with the challenge of displaying items in a list correctly. I am working on converting the timestamp attached to each item into a readable format. For instance, 1475842129770 is transformed into Friday, 07.09.2016 ...

Unraveling the mysteries of webpack configuration

import * as webpack from 'webpack'; ... transforms.webpackConfiguration = (config: webpack.Configuration) => { patchWebpackConfig(config, options); While reviewing code within an Angular project, I came across the snippet above. One part ...

Only after the code is executed again will $.get function start working

On my main page, I have a select element that is populated from a database with the following code: <select id="report" class="selectpicker input-sm"> <option value=''>Choose a date</option> <?php foreach($this->r ...

Activate tooltip when hovering over the <img> element

I am having trouble implementing a tooltip using CSS for an <img> element. While I have successfully added a tooltip to <a href> </a> tags, applying the same functionality to an <img> tag has proven difficult. http://jsfiddle.net/ ...

Tips on incorporating the vue package

I recently started using Vue and decided to incorporate a plugin called vue-toastr. I'm attempting to integrate it with Laravel, which already has Vue set up with the plugin. Unfortunately, I keep encountering an error that says "TypeError: Cannot rea ...

Issue with background image resizing when touched on mobile Chrome due to background-size property set to cover

My current challenge involves setting a background image for a mobile page using a new image specifically designed for mobile devices. The image is set with the property background-size: cover, and it works perfectly on all platforms except for mobile Chro ...

What is the hexadecimal color code for a specific element's background?

How can I retrieve the background color code of a specified element? var backgroundColor = $(".element").css("background-color"); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="elemen ...

What is the process for displaying a document file in an iframe that is returned from a link's action?

I have a main page called index.cshtml. This page displays a list of document files along with an iframe next to it. My goal is to load the selected document file into the iframe when I click on any item in the list. However, currently, when I click on a d ...

Exploring the role of 'this' within a given setting

Despite my efforts, I am struggling to comprehend why 'this' is not functioning in the first code sample but works in the second one. First (doesn't work here) $('#' + ID).parent().siblings().each(function() { selectChildren( ...

Learn the process of adding values to HTML forms within ExpressJS

I am facing a challenge in injecting Javascript variable values into HTML forms on an Expression JS server. I am unsure about how to solve this issue. All I want to do is insert the values of x, y, and res into the forms with the IDs 'firstvalue&apos ...

Error occurs when trying to map an array within an asynchronous function

Hey there, I have an array of objects with validation inside my async function (router.post()) and I need to map it before validating. Here is the approach I am taking: ingredients.map(({ingredient, quantity})=>{ if(ingredient.trim().length < 1 | ...

Encountered an error: "switch/mergeAll/flatten is not a valid function" when working with the http driver

As I delve into learning CycleJS, one thing that has caught my attention is the usage of Cycle's HTTP Driver. It seems that in order to reach the stream level, merging the response stream stream with RxJS switch/mergeAll is essential. However, when at ...

drawing tool with JavaScript and HTML5

Can you help me figure out why my sketchpad isn't working in Processing.js? I've checked my code and there are no errors, but the canvas is not showing up. Any suggestions? Take a look at the code below: function createSketchPad(processing) { ...