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

Autoprefixer encountered a TypeError while executing the npm script: Patterns must be specified as a string or an array of strings

Upon executing npm run prefix:css, the following error message is displayed: Error: Patterns must be a string or an array of strings { "name": "natours", "version": "1.0.0", "description": "A project for natours", "main": "index.js", "script ...

Incorporating Bootstrap into a fresh project

I'm completely new to the world of development, so I'll do my best to phrase this question correctly! Last month, I successfully created my first web application using RoR and Bootstrap for some visual enhancements. Currently, I am working on a ...

How is it possible to encounter a missing semicolon CssSyntaxError during a Gatsby build?

Currently, I am in the process of developing a Gatsby page with Material UI. The design of the page is almost finalized; however, upon completing the project, an unexpected build error occurs when running npm run build. WebpackError: Pathname: /invitation/ ...

Aptana Studio 3 fails to detect PHP code embedded within an .html file

When writing code, such as: <!DOCTYPE html> <html> <body> <?php echo "My first PHP script!"; ?> </body> </html> After <?php, I am encountering a red X icon error indicating that a ">" is missing at the end of the ...

Displaying JavaScript Array Elements in an Aligned Table Format

Unfortunately, I accidentally deleted my previous post and now need to repost it. I have a code that is functioning well, thanks to the great help I received here. However, the output of the array players is not neatly aligned with each other in a table f ...

Retrieving the Vue component object while inside the FullCalendar object initialized within the component

When using Full Calendar with VueJS, I ran into a problem where I needed to open a custom modal when clicking on a time slot in the calendar. The issue was that I couldn't call a function outside of the Full Calendar object to handle this. This is bec ...

Tips for including descriptions on individual images within the jQuery PhotoStack gallery

Recently, I encountered a challenge. I am currently utilizing a PHP file for accessing images stored in a specific folder. Here is the snippet of my PHP code: <?php $location = 'albums'; $album_name = $_GET['album_name']; $files ...

Is there a possibility of Typescript expressions `A` existing where the concept of truthiness is not the same as when applying `!!A`?

When working with JavaScript, it is important to note that almost all expressions have a "truthiness" value. This means that if you use an expression in a statement that expects a boolean, it will be evaluated as a boolean equivalent. For example: let a = ...

The animation in Material UI does not smoothly transition with the webkit scrollbar

I've been experimenting with CSS animations in Material UI's sx property to achieve a webkit scrollbar that eases in and out. However, instead of the desired effect, the scrollbar appears and disappears instantly. Whether I define the keyframes ...

Updating a value using jQuery AJAX techniques

Using jQuery AJAX, I am loading the content of a page in this way: $(document).ready(function(){ $('#next').click(function(event){ $.ajax({ url: "load.php?start="+$('#lastid').text(), success: function(html){ $("#results"). ...

Azure API Management encountering Cross-Origin Resource Sharing (CORS) problem

Utilizing Azure API management to interact with my Python Flask web service has been efficient for GET operations. However, encountering an issue when attempting POST requests using jQuery AJAX calls where the request is converted to OPTIONS resulting in t ...

javascript creating unique model instances with mongoose

I've searched through related posts without finding exactly what I need. Currently, I am working on building a backend rest API and conducting tests to collect data. Each test has its own model which is associated with collections in the database. T ...

Accessing the current window dimensions using CSS

I'm experimenting with a way to set the background of the entire body element. <html> <head><head/> <body> <div class="wrapper"></div> </body> </html> How can I determine the height and width of the cu ...

Exploring ways to customize the selected text color within a jQuery mobile division using CSS

Is there a way to change the color of selected text to #3C3 for the div one only and not for div two? <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link ...

Encountered an error while trying to create module kendo.directives using JSPM

I am attempting to integrate Kendo UI with Angular in order to utilize its pre-built UI widget directives. After running the command jspm install kendo-ui, I have successfully installed the package. In one of my files, I am importing jQuery, Angular, and ...

Scrolling Down on a Jquery Login Page

I am currently utilizing JQuery version 1.4.2. My objective is to allow the user to scroll down to the login form by clicking on the 'login' option in the top menu, similar to Twitter's design. The implementation works impeccably with insert ...

Encountered an error: Trying to access 'location' property from undefined in express app

Encountered Issue : I am facing an error that is hindering me from fetching data from the HTML form. Every time I input values into the form and click the button to send the data to an API for saving it in the database, this error pops up: Cannot read pro ...

Having difficulty sending data to a controller through AJAX in Code Igniter. Can anyone help troubleshoot

I recently started learning PHP OOP and am currently using the Code Igniter framework. I encountered some difficulties in sending data to the controller using AJAX, so I attempted a simple test to check if AJAX was functioning properly, but unfortunately, ...

Guide on how to programmatically assign a selected value to an answer using Inquirer

Currently, I'm utilizing inquirer to prompt a question to my users via the terminal: var inquirer = require('inquirer'); var question = { name: 'name', message: '', validation: function(){ ... } filter: function( ...

Navigating to User's Specific Info Page using Node.js

Would love some input on my current issue. I have a table featuring a list of users, and my goal is to display user information in detail whenever a user (which corresponds to a row in the table) is clicked. The process involves identifying which user was ...