Having trouble getting the jQuery toggle function to work properly with div elements

I'm struggling with some divs that are not behaving as expected. When I click the button, it opens and changes the text to "Collapse", but when I click it again, it doesn't change the text back to "Find Support". Additionally, if I click on a different div, it will close the open one first before opening the new one. I need all divs to close when one is clicked, and for the text to change from "Collapse" to "Find Support". Can anyone offer some guidance?

$(document).ready(function () {
        $(".findButton").click(function () {         

            $(".findButton").val('Find Support');            
            $(this).val('Collapse');
            $("id^=ilsList").remove();
            $(this).closest($("#ilsList").appendTo($(this).parent()).slideToggle());          
       });        
    });

Check out the demo here.

Answer №1

Is this what you were looking for? http://jsfiddle.net/sw0zn36e/7/

<div class="DivGroup">
    <label>Bay Village Branch - <b>Cuyahoga County Public Library</b></label>
    <input type="button" onclick="LoadBranches('CuyahogaCounty')" value="Find Support" class="findButton"/>
    <div class="IlsTable" style="display:none;">
        <img src="../../Content/Images/ajax-loader.gif" style="display: none;" id="ajaxLoader"/>
        TEXT            TEXT    TEXT    
    </div>
</div>

$(document).ready(function () {
    $(".findButton").on('click', function () {
        if($(this).val() == 'Find Support') {
            $(".findButton").val('Find Support');            
            $(this).val('Collapse');
            $('.DivGroup').find('.IlsTable').slideUp();
            $(this).closest('.DivGroup').find('.IlsTable').slideDown();                        
        }
        else {
            $(".findButton").val('Find Support');
            $('.DivGroup').find('.IlsTable').slideUp();
        }                    
    });    
});

UPDATED to tackle the challenge of toggling divs. I made some modifications to the HTML structure to enhance the usability of classes. Hopefully, this solution resolves your issue!

Answer №2

~~~~~~~~~~~~~~~~~~~2nd REVIEW ~~~~~~~~~~~~~~~~~~~~~~

Alright, take a look at the revised response below:

~~~~~~~~~~~~~~~~~~~2nd REVIEW ~~~~~~~~~~~~~~~~~~~~~~

Sure, give this modification a try:

$(document).ready(function () {
$(".findButton").click(function () {      
    if ($(this).val() == 'Find Support') {   
        $('.findButton').each(function(){
            $(this).val('Find Support');
        });
        $(this).val('Collapse');            
        $(this).closest($("#ilsList").appendTo($(this).parent()).slideDown());            
    } else {       
        $(this).val('Find Support');
        $('.IlsTable').not(this).each(function(){
             $(this).slideUp();
        });
    }                
});  
});

http://jsfiddle.net/sw0zn36e/4

Answer №3

To determine if a button's previous value is different from its current one, you can use the following code snippet:

$(".findButton").click(function() {

  $(this).val(function() {
    return $(this).val() === 'Collapse' ? 'Find support' : 'Collapse';
  });
  $("id^=ilsList").remove();
  $(this).closest($("#ilsList").appendTo($(this).parent()).slideToggle());
});
#ilsList {
  display: none;
}
.IlsTable {
  background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
  clear: both;
  margin-left: 100px;
  margin-right: 100px;
  padding: 20px 60px 20px 20px;
  position: relative;
}
.trResult,
.DivGroup {
  border-top: 1px solid #ccc !important;
  float: left;
  padding: 3px 0 10px !important;
  width: 100%;
}
.DivGroup {
  padding-bottom: 3px;
}
.DivGroup label {
  font-size: 17px;
}
button:hover,
.findButton {
  border: 1px solid #333;
  color: #333;
}
button,
.findButton {
  background: none repeat scroll 0 0 rgba(0, 0, 0, 0) !important;
  border: 1px solid #999 !important;
  border-radius: 3px;
  color: #666 !important;
  cursor: pointer;
  font-weight: normal !important;
  margin-top: 5px;
  padding: 5px !important;
  text-transform: uppercase;
}
.findButton {
  background: none repeat scroll 0 0 #1d6096;
  border: medium none;
  border-radius: 5px;
  color: #fff;
  cursor: pointer;
  float: right;
  font-weight: bold;
  padding: 8px 21px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div class="DivGroup">
  <label>Bay Village Branch - <b>Cuyahoga County Public Library</b>

  </label>
  <input type="button" onclick="LoadBranches('CuyahogaCounty')" value="Find Support" class="findButton">
</div>
<div class="DivGroup">
  <label>Bay Village Branch - <b>Cuyahoga County Public Library</b>

  </label>
  <input type="button" onclick="LoadBranches('CuyahogaCounty')" value="Find Support" class="findButton">
</div>
<div class="DivGroup">
  <label>Bay Village Branch - <b>Cuyahoga County Public Library</b>

  </label>
  <input type="button" onclick="LoadBranches('CuyahogaCounty')" value="Find Support" class="findButton">
</div>
<div class="IlsTable" id="ilsList">
  <img src="../../Content/Images/ajax-loader.gif" style="display: none;" id="ajaxLoader">TEXT TEXT TEXT</div>

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

the scrollbars are appearing on the window instead of within my div container

I'm having trouble getting a scrollbar on my div element when the content is too wide. Instead, the scrollbar always appears on the window itself. I have tried adjusting the overflow settings but can't seem to find the right solution. jsFiddle ...

Customize Your Chrome Experience: Inject an Element to Open a Hidden HTML Page within the Extension

I am currently working on a Chrome extension and I would like to add a link tag into an object, which will then direct the user to an about page stored within the extension itself. Is there a way to achieve this? Below is the code from my content.js file: ...

Fade in an image using Javascript when a specific value is reached

Here's the select option I'm working with: <div class="okreci_select"> <select onchange="changeImage(this)" id="selectid"> <option value="samsung">Samsung</option> <option value="apple">App ...

In Javascript, an error occurs when something is undefined

I've been grappling with a Javascript issue and seem to have hit a roadblock. In Firefox's console, I keep encountering an error message that says "info[last] is undefined," and it's leaving me puzzled. The problematic line appears to be nu ...

Creating custom Magento extensions with jQuery integration

I am in the process of developing my very first extension that I plan to market on magento connect. One of the requirements for my module is jquery. The dilemma I face is if I include jquery in my module, it could potentially overwrite a version that the ...

Tips for positioning a div element at the center in ASP.NET

I have written the following code snippet: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transit ...

I'm struggling to understand how to use the Beautifulsoup find() function with this particular HTML code

I have been attempting to extract specific information from a webpage using Python and Beautiful Soup, but I am struggling to correctly identify the path to the data I need. Here is an example of the HTML code: <div class="operator active" data-operato ...

What steps can be taken to create a progress bar in the input field that spans the entire width of its parent div, reaching

I received assistance from a friend in creating this progress bar. Everything seems to be working well, except for the fact that the progress bar is not extending to the full width of the parent div. The new width after each input tag is entered using Java ...

Using jQuery to Convert CSV Data into an HTML Table

I've been exploring the incredible jquery.csvtotable.js plugin for converting csv files to html tables. One question that has been on my mind is how can I allow users to select a .csv file using a browse button that isn't a server-side control? ...

Unlocking the Potential of Larger jQuery Icons

In a recent announcement, the jQuery team unveiled new icons for their UI components. However, I have been unable to locate any examples of how to use them or where they can be downloaded from. Also, it appears that the themes in the ThemeRoller only provi ...

Using fixed positioning in CSS caused a malfunction in the layout of Materialize columns

Looking to develop a unique sidenav menu design for Large and Medium screens using Materialize. After referring to the example provided in the Materialize documentation, I successfully implemented it and found it cool! However, my goal is to have the Sid ...

Implementing Checkbox Functionality within a Dropdown Menu using AngularJS or JavaScript

I am interested in finding a multi-select checkbox solution similar to the one demonstrated here: Instead of using jQuery, I would prefer options in AngularJS or pure JavaScript. Does such a solution already exist in these frameworks, or is there guidance ...

I'm experiencing trouble with Shopify as it appears to be failing to execute

I have been attempting to incorporate a tracking pixel into my project. Thus far, I have tested the code in various environments, both with and without wrapping it in a function and deploying it using window.onload. If you would like to see the implementa ...

Having trouble aligning page in the center with flexbox and styled components

Hey there, I'm having some trouble figuring out how to center my page using flexbox within styled components in a Next.js app. Any suggestions or tips? Let me share with you my Blog Component import ReactMarkdown from 'react-markdown' impor ...

Two child DIVs within a single parent DIV

I am struggling to keep two divs side-by-side within a parent div. Initially, I was able to position the image in the right div successfully, but when resizing the elements, everything became disorganized and I can't seem to fix it. As a beginner, I w ...

How can multiple buttons be added to each row in Jquery datatables and how can events be applied to them?

I currently have one button, but I am in need of two buttons. These buttons will trigger MySql commands when clicked to retrieve data from the database. How can I set up an event handler for these buttons? $(document).ready(function () { var table= ...

Is it possible for Jquery to directly retrieve the form input without the need for a SET button in the script?

I have a script that functions as a basic countdown. All you need to do is enter a number, press the SET button, and click START for the countdown to begin. Although I primarily use this script in the gym, I often forget to hit the SET button after enteri ...

Changing the CSS property of a single table cell's innerHTML

I have a question that may seem silly, but I'm going to ask it anyway. Currently, I am iterating through a list of strings that follow the format "1H 20MIN" and adding them to table cells using the innerHTML property like so: for (i = 0; i < list ...

Is there a way to get an iframe to mimic the behavior of other media elements within a horizontal scrolling container?

Take a look at this code snippet: $(document).ready(function() { $('.scrollable-area').on('wheel', function(e) { var scrollLeft = $(this).scrollLeft(); var width = $(this).get(0).scrollWidth - $(this).width(); var delta ...

Updating variable values in AngularJS while navigating through routes

How can I dynamically set the flag icon inside the page header based on the selected language using AngularJS? The language selection is done in a separate .htm file and all managed by AngularJS routing. My application has a single controller called "appCo ...