Using Jquery to toggle the visibility of divs based on radio button selections

I am struggling to hide the divs with the radio buttons until their title is clicked on. However, I am facing issues as they are not showing up when their title is clicked on. Any assistance would be greatly appreciated.
SPIKE

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8>
    <meta name="generator" content="CoffeeCup Web Editor (www.coffeecup.com)">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <link href="mebbe2.css" rel="stylesheet"/>

    <style>
        body {
            background-color: #7094B8;
        }

        .container {
            position: relative;
            left: 24%;
        }

        h1 {
            align-text: center;
        }

        #butt {
            display: block;
            background-color: #e5eecc;
            height: 230px;
            width: 500px;
            padding: 3px;
            border: solid 2px #c3c3c3;
        }

        p {
            font-weight: bold;
            font-size: 22px;
        }
    </style>
</head>
<body>
<div class="container">
    <div id="select">
        <h1>Select Your Preferred Search</h1>

        <p>Click on your choice of search method.</p>
        <button id="communities">Communities Served</button>
        <br>
        <button id="specialties">Therapeutic Specialties</button>
        <br>
        <button id="insurance">Insurance Accepted</button>
        <script type="text/javascript">
            $(document).ready(function () {
                $(".special,.commune,.insure").hide();
            });
        </script>

        <script>
            $(document).ready(function () {
                $("#communities").click(function () {
                    $(".select, .special, .insure").hide();
                });
                $("#show").click(function () {
                    $(".commune").show();
                });
            });
            $(document).ready(function () {
                $("#specialties").click(function () {
                    $(".select,.insure,.commune").hide();
                });
                $("#show").click(function () {
                    $(".special").show();
                });
            });
            $(document).ready(function () {
                $("#insurance").click(function () {
                    $(".select,.special,.commune").hide();
                });
                $("#show").click(function () {
                    $(".insure").show();
                });
            }); </script>
    </div>
    <div class="commune" id="butt"><h3>Choose Your Community</h3>

        <form action="input">
            <input type="radio" name="common" value="caucasion">Caucasion<br>
            <input type="radio" name="common" value="hispanic">Hispanic<br>
            <input type="radio" name="common" value="carribean">Carribean<br>
            <input type="radio" name="common" value="africanAmerican">African-American<br>
            <input type="radio" name="common" value="male">Male<br>
            <input type="radio" name="common" value="female">Female<br>
            <input type="radio" name="common" value="professional">Professional<br>
            <input type="radio" name="common" value="lGBT">LGBT<br>
            <input type="radio" name="common" value="youth">Youth
        </form>
    </div>
    <div class="special" id="butt">
        <h3>Choose Your Specialty</h3>

        <form action="input">
            <input type="radio" name="spec" value="groups">Groups<br>
            <input type="radio" name="spec" value="couples">Couples<br>
            <input type="radio" name="spec" value="abuse">Abuse<br>
            <input type="radio" name="spec" value="addictions">Addictions<br>
            <input type="radio" name="spec" value="loss">Loss<br>
            <input type="radio" name="spec" value="depression">Depression<br>
            <input type="radio" name="spec" value="family">Family Conflict<br>
            <input type="radio" name="spec" value="esteem">Self Esteem
        </form
    </div>
    <div class="insure" id="butt">
        <h3>Choose Your Insurance </h3>

        <form action="input">
            <input type="radio" name="insur" value="harvard">Harvard Pilgrim<br>
            <input type="radio" name="insur" value="blueCross">Blue Cross and Blue Shield<br>
            <input type="radio" name="insur" value="tufts">Tufts<br>
            <input type="radio" name="insur" value="healthNet">Health Net<br>
            <input type="radio" name="spec" value="loss">Loss<br>
            <input type="radio" name="insur" value="humana">Humana<br>
            <input type="radio" name="spec" value="loss">Loss<br>
            <input type="radio" name="insur" value="cigna">Cigna
        </form>

    </div>

</body>
</html>

Answer №1

It seems like you may be interested in this helpful solution. http://jsfiddle.net/Xqm77/1/

$(document).ready(function () {
    $(".special,.commune,.insure").hide();
    $("#communities").click(function () {
        $(".select, .special, .insure").hide();
        $(".commune").show();
    });
    $("#specialties").click(function () {
        $(".select,.insure,.commune").hide();
        $(".special").show();
    });
    $("#insurance").click(function () {
        $(".select,.special,.commune").hide();
        $(".insure").show();
    });
});

However, there is a way to streamline this code by adding data attributes to the buttons. This will allow you to determine which section to display using one click function. Here's an example:

<button id="communities" data-show="commune">Communities Served</button>

By implementing this method, you can utilize a single click event function. View it live here: http://jsfiddle.net/Xqm77/2/

$(document).ready(function () {
    $(".container > div:not(#select)").hide();
    $("#select button").click(function() {
        var $this = $(this),
            toShow = $this.data("show");
        $(".container > div:not(#select)").hide().filter("."+toShow).fadeIn("slow");
    });
});

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

Executing a Particular Function in PHP with Arguments

As a newcomer to PHP and JavaScript, I'm attempting to invoke a specific function in a PHP file from JavaScript. Here is my code: <script> function load($dID) { $.ajax({ url: "myPHP.php", ...

Is the iPhone height incorrect using jQuery?

When setting the viewport tag to 480px on an iPhone in portrait mode, the true dimensions of the website are as follows: 480x534px when the top bar is visible 480x594px when the top bar is hidden However, despite setting the viewport size correctly, fun ...

Using Electron to Show a Video Stored in the Local File System Using a Custom Protocol

Currently, I'm facing challenges while using electron to develop a basic video display application. Despite my efforts, I am struggling to show a video in the renderer by correctly implementing the registerSchemesAsPrivileged method. Although there ar ...

javax.el.MethodNotFoundException: JSP method cannot be located

While compiling the JSP template, I encountered the following exception: The method getFriendImage() is not recognized I attempted to rebuild the class without success. The src attribute contains a valid link to an image that is functioning properly. ...

Reactjs may have an undefined value for Object

I have already searched for the solution to this question on stackoverflow, but I am still confused. I tried using the same answer they provided but I am still getting an error. Can someone please help me resolve this issue? Thank you. const typeValue = [ ...

Why doesn't jQuery ajax work when sourcing the URL from a variable but works with a hard-coded URL?

During my experimentation with setting and getting the URL for a jQuery Ajax (post) call, I realized the value of dynamically setting the destination URL for the request. Here's how I attempted to achieve this: To set the URL to 'store.php' ...

What is the best way to adjust word spacing in Bootstrap?

https://i.sstatic.net/E7diW.jpg The problem persists even after trying to create a class and assign it to the ul tag. .spacing { word-spacing: 100px; } <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protecti ...

Issue arise when utilizing async/await in conjunction with .forEach method

My code is attempting to make a basic request to Redis in order to retrieve all the values (not keys) from my database. However, I am encountering an issue where the tab is being returned before the .forEach function even begins. This is evident because ...

Tips on adjusting the width of a div container based on the size of an

I am struggling with setting the width of my "picBox" div equal to the image tag inside. I have tried adjusting the CSS code but cannot seem to get it right. Any suggestions on how to solve this issue would be greatly appreciated. Thank you. The CSS code: ...

The date formatting issue persists in jqGrid 3.5 as the dates are not being displayed correctly

I have been experimenting with jqGrid 3.5.2 and currently, I am extracting data from a local JSON object. Upon researching date formatting, I realized that the dates in my JSON were not properly formatted, so I converted them all to ISO8601Long format. Now ...

The stylesheet malfunctioned while attempting to load varying layouts in separate controllers

Hey, I encountered a strange issue. After clicking on the three pages displayed in the images below: Step 1 controller welcome Step 2 other controllers Step 3, back to step 1 view controller welcome The final image, when returning to controller welcom ...

React-select allows for multiple selections within a component when the onChange function

I am currently utilizing react-select. Per the official documentation, it recommends using 'isMulti' to select more than one option. Below is the custom component that I have created. import React from 'react'; import { Form } from &ap ...

Guide on integrating AJAX with servlets and JSP for database-driven applications

What is the best way to integrate AJAX technology with servlets and JSP for a database application? I am currently developing a JSP page that makes calls to JavaScript. The JavaScript then calls a servlet where the database is accessed. How can I pass th ...

What is the best approach to unit testing this React Component?

I have created a component that acts as a wrapper for another component. My question is, how should I approach unit testing for this component? Besides checking the state and method calls to ensure they update the state correctly. Other than rendering pro ...

Having trouble with your HTML iFrame not functioning properly?

My code is not working as expected and I'm puzzled by it. It was working fine yesterday but now it's giving me trouble. <iframe allowfullscreen="" allowtransparency="true" frameborder="0" height="2000" scrolling="no" src="http://www.google.co ...

Eliminate custom CSS generated by themes on WordPress category pages

I am a newcomer to the world of Wordpress and I'm currently in the process of making adjustments to an existing theme. The theme itself is generating custom CSS code and adding it to the wp_head section. add_action('wp_head', 'custom_c ...

Arrange the input fields on a single line in Rails for a clean and organized layout

Below is a Rails code snippet for input fields. Name:-<%=text_field_tag "specification[name1]","",:class=>"autocomplete form-control"%> <br/> Value:-<%=text_field_tag "specification[value1]","",:class=>"autocomplete form-control"%> ...

Encountering difficulties with displaying error messages on the Material UI datepicker

I am currently utilizing React Material UI There is a need to display an error based on certain conditions that will be calculated on the backend. Although I have implemented Material UI date picker, I am facing issues with displaying errors. import * as ...

Tips for showcasing all values in a nested array

In my Vue application, I am dealing with a nested array where users can select one date and multiple times which are saved as one object. The challenge I am facing now is how to display the selected date as a header (which works fine) and then list all the ...

Guide on displaying dynamically generated values of form fields in Selenium using Eclipse

My goal is to display the dynamically generated "Shipping rate" of certain products in the Cart. For example, once a product is added to the cart and during the checkout process after the user enters their shipping address, the SHIPPING RATE is dynamical ...