Unveiling concealed content with JQuery

Here is a link to my pen: http://codepen.io/anon/pen/IszKj

I am looking to achieve the functionality where clicking on either "any status" or "any date" will reveal a hidden div containing a list of options.

My main query is about the best approach to take in implementing this feature.

  1. Should I have two separate divs and open the one corresponding to the clicked list item?
<div id="status" style="display: hidden">Option 1 Option 2</div>
<div id="date" style="display: hidden">Option 1 Option 2</div>
  1. Alternatively, should I use a single div and display only the relevant content for that button click?
<div style="hidden">
<span id="status">...</span> 
<span id="date">...</span> 
</div>

Moreover, I'm unsure whether to utilize toggle or the conventional open / close function.

Lastly, it would be ideal if the functionality gracefully degrades when JavaScript is disabled.

Answer №1

Here's a Fiddle I created for you:

http://jsfiddle.net/e4mQD/2/

HTML:

<div style="display: block;
height: 40px;">
    <ul id="filter">
        <li>
            <span>Any category▾</span>
            <ul class="options">
                <li>Category1</li>
                <li>Category2</li>
            </ul>
        </li>
        <li>
            <span>Any date▾</span>
            <ul class="options">
                <li>DateA</li>
                <li>DateB</li>
            </ul>    
        </li>
    </ul> 

CSS:

#filter, .options {
  list-style-type: none;
}

.options {
  display:none;
}

.options li {
    cursor: pointer;
}

JavaScript:

$('#filter li').click(function(){
  $(this).find('.options').toggle();
});

Answer №2

visibility: none is not a proper CSS style. Please use display: none

Answer №3

How should I best approach this situation?

For your specific scenario, it's advisable to utilize separate blocks for each option.

As suggested by @mh-itc, consider using nested lists (ul) instead of div within the li elements.

Additionally, opt for using an anchor tag (a) rather than a span element.

It would be beneficial if the functionality degrades gracefully without JavaScript.

You can achieve this by delaying the application of "display:none;" until after the JavaScript has loaded and executed.

Demo: http://jsfiddle.net/abhitalks/928Dj/

Markup:

<div>
    <ul id="filter">
        <li>
            <a href="#" class="dropdown">Any status ▾</a>
            <ul class="opt">
                <li><label><input type="checkbox" />Status 1</label></li>
                <li><label><input type="checkbox" />Status 2</label></li>
            </ul>
        </li>
        <li>
            <a href="#" class="dropdown">Any date ▾</a>
            <ul class="opt">
                <li><label><input type="checkbox" />Date 1</label></li>
                <li><label><input type="checkbox" />Date 2</label></li>
            </ul>
        </li>
    </ul>
</div>

CSS:

ul {
    margin: 0; padding: 0;
    list-style: none;
}
#filter {
    display: inline-block;
}
#filter > li {
    display: inline-block;
    margin-bottom: 12px;
    padding-right: 12px;
    vertical-align: top;
}
ul.opt.hidden {
    display: none;
}

jQuery Code:

// comment out to see how it degrades without javascript
$("ul.opt").addClass("hidden");
$('#filter > li > a').on("click", function(e) {
      $(this).next('ul').toggle();
});

Note: In the provided demonstration, uncomment the JavaScript code to observe its behavior when JavaScript is enabled. Conversely, comment it out to witness degradation in the absence of JavaScript support.

Answer №4

To ensure accessibility, consider changing the hidden status using JavaScript when loading the site. By doing this, users with add-ons like NoScript enabled will be able to view all options without sacrificing functionality. Those who use NoScript typically prefer not to disable it in order to use a website properly.

One solution is to utilize two separate divs for each option box. This way, both boxes can be displayed simultaneously and clearly differentiated with a styled version. Assign a class such as "optionbox" to these divs and apply your CSS rules there instead of creating a rule with #date, #status

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

Balanced Columns with Background Extending Beyond Half of the Page

If you're looking for the final code, it can be found here: http://jsfiddle.net/asaxdq6p/6/ I am attempting to create an effect similar to this image: To achieve this, I am using the Equal Height Columns with Cross-Browser CSS trick as described in ...

Style Vue slots one by one

I am a beginner delving into the world of vue (specifically, vue 2) and aiming to grasp the concept of utilizing slots in the most effective manner. Below is the code I'm working with: <template> <div> <button cla ...

Adding a Video as a Background Button Using HTML and CSS

My attempt to utilize background-image in CSS for the button background was unsuccessful as it only supports images, not videos. Is there a way to set a video as the background of a button? My goal is to create a button with a continuously looped muted v ...

Is there a method in JavaScript/jQuery to gently push or flick the scroll so that it can be easily interrupted by the user? Further details are provided below

I am looking for a solution in javascript that will allow the page to automatically scroll down slightly, but I also want the user to be able to interrupt this scroll. When using jquery for auto-scrolling, if the user begins manual scrolling while the ani ...

Steps to delete an item from a table without affecting the database

I've successfully set up a mysql database and integrated it with my HTML table using AJAX to both save data in the db and retrieve data from it. Additionally, I have implemented a feature where each record in the HTML table has a remove button that us ...

Troubleshooting problem with Bootstrap Modal inputs and header tags

When it comes to the button that says data-whatever="Add a recipe", I think it is causing my input placeholders to be overridden, and I'm not sure why. If I remove it, everything works but the h5 header for the new recipe will be blank. <button ...

Incorporating Selenium Python to display text results in a single-line format for enhanced output presentation

Looking for a way to extract data from the following HTML code: <div class="main"> <div class="firstdigitinside"> <span class="firstdigit"> <span class="firstdigithere>1 ...

Calculating distinct values within a single key in an object

My goal is to track the occurrences of four specific string values within the same key. The issue lies in my struggle with adding multiple counters. While the first counter successfully tracks the initial condition, subsequent conditions within the if/els ...

Guide on retrieving the ID after a new entry is added using a trigger in Express.js and MySQL

Currently, I am utilizing express and workbench to set up a database for handling the creation, viewing, and updating of cars. When I make a POST request to add a new car, it generates a new entry with details such as manufacturer, model, and price. I hav ...

Manipulating variables in the main controller scope from a function in a transcluded directive scope using AngularJS

How can parent controller scope variables be updated from a transcluded directive scope's function? I have a situation where I am including directives within another directive using transclusion. Here is an example of how it is set up: <my-table& ...

Dealing with the problem of delayed image loading in AngularJS due to a setTimeout conflict

The issue: Upon page load, some images are still processing and not displaying (even though the image URLs are known). The resolution: To address this problem, a customized directive was developed to showcase a loading spinner as a placeholder until the i ...

Issues with HTML keycodes not functioning properly in Firefox

Here is the code I am using: function restrictInput(e) { var charCode = (e.which) ? e.which : ((e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : 0)); if((charCode < 48 || charCode > 57) && ( ...

Having trouble finding a solution because Ajax/JSON is only retrieving a single result from the PHP/SQL file

I am facing an issue with retrieving data through AJAX/JSON from another file containing a PHP While loop. The problem is that only one data is being returned instead of the expected 11 rows. Despite researching extensively on Google, YouTube, and other so ...

Comparing timestamps in MySQL with those in jQuery: A guide

Recently, I retrieved a timestamp value from a MySQL table. My next task involves comparing this value with a jQuery timestamp to determine if the time and date have expired or not. ...

Issue with jQuery .hover() not functioning as expected

The issue I'm encountering is just as described in the title. The code functions correctly on all divs except for one! $(".complete-wrapper-1").hide(); var panelHH = $(".file-select-wrapper").innerHeight; $(".files-button").hover(function(){ $(" ...

Adjust the number of columns displayed when rendering with Datatables

I've utilized DataTables to create a dynamic datatable. $('table').DataTable( { dom: 'Bfrtip', buttons: [ 'copy', 'excel', 'print', ], responsive: true } The table I created c ...

An effective way to determine the size of a browser through javascript

In an effort to enhance the responsiveness of a website, I have included the following code snippet on one of my pages: document.write(screen.width+'x'+screen.height); However, I am encountering an issue where the code displays my screen resolu ...

Utilize Bootstrap cards to display images across the full width of the page

I am using the Bootstrap card component https://getbootstrap.com/docs/4.0/components/card/ to display an image. The issue I'm facing is that on the Bootstrap template, it adds a margin around the image. I would like the image to take up the full width ...

Exploring jQuery Mobile: Uncovering the Power of clientX, clientY, and taphold Event

In my current project, I am implementing the taphold event and require the coordinates of the tapped point by the user. However, I have encountered an issue where event.clientX and event.clientY are returning as undefined (you can see the example here: her ...

Can you guide me on how to interact with a table value using Selenium WebDriver?

I am having difficulty selecting a cell within a table. The cell does not seem to be recognized as a clickable object when I attempt to locate the element. I have tried different approaches to referencing the element, but all lead to the same outcome. I am ...