npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jquery.toggle-visibility

v1.0.1

Published

A jQuery plugin to declaratively toggle the visibility of elements based on form input selections.

Downloads

20

Readme

jquery.toggle-visibility

A jQuery plugin to declaratively toggle the visibility of elements based on form input selections.

The general philosophy here is that declarative is better than imperative. Using this plugin, I was able to remove a bunch of Javascript that performed simple show/hide operations based on form selections.

Using nesting, you can make some pretty complex interactions possible without writing any code at all – you just need to add the proper data-* selectors. However, this is really meant for simple interactions – anything too complex, and you might want to consider a real framework, such as Vue or React.

See usage examples by cloning the repository and opening test.html.

Installation

npm

npm install jquery.toggle-visibility --save

yarn

yarn add jquery.toggle-visibility

Note: In the cases of npm and yarn, the source file will be in node_modules/jquery.toggle-visibility/src/jquery.toggle-visibility.js. The project doesn't include a build step, as everyone has their own way of setting that up at the project level these days, minifying and whatnot.

manual

Just download the source file here, and place a referece to it in your document's head. Make sure to include jQuery first.

Usage

This plugin uses declarative data-* attributes to determine what is to be shown or hidden, depending on the selection the user has made.

To activate the plugin, call it on all the relevant inputs as a jQuery plugin once the DOM is ready. For example:

$(function() {
  $('input[data-toggle-element], select[data-toggle-element]').toggleVisibility();
});

Currently, the plugin hides elements by simply adding the "hidden" class to them, so you should have something like the following in your CSS:

.hidden { display: none; }

If you are using Bootstrap, this utility class should already be present.

Checkbox

Simply give a checkbox input a data-toggle-element of the selector you wish to hide/show as that checkbox is checked/unchecked.

<form>
  <label>
    <input type='checkbox' data-toggle-element='.checkbox-is-checked'>
    Check me
  </label>

  <div class='checkbox-is-checked'>The checkbox is checked!</div>
</form>

You can also invert the checkbox behaviour (hide on checked) by setting the data-toggle-element-invert of the target elements to true, like so:

<form>
  <label>
    <input type='checkbox' data-toggle-element='.checkbox-is-unchecked'>
    Check me
  </label>

  <div class='checkbox-is-checked' data-toggle-element-invert='true'>
    The checkbox is not checked!
  </div>
</form>

Radio

Give all related radio button inputs a data-toggle-element of the selector that selects all the possible elements you wish to show/hide based on the value of the radio button. There should be one element per possible value of the radio button.

Give each of the elements that you wish to show/hide a data-toggle-element-value that corresponds to a possible value of the aforementioned radio button group.

<form>
  <label>
    <input type='radio' name='radio-buttons' value='one' data-toggle-element='.radio-button-selections'>
    One
  </label>
  <label>
    <input type='radio' name='radio-buttons' value='two' data-toggle-element='.radio-button-selections'>
    Two
  </label>
  <label>
    <input type='radio' name='radio-buttons' value='three' data-toggle-element='.radio-button-selections'>
    Three
  </label>

  <div class='radio-button-selections' data-toggle-element-value='one'>Selection: One</div>
  <div class='radio-button-selections' data-toggle-element-value='two'>Selection: Two</div>
  <div class='radio-button-selections' data-toggle-element-value='three'>Selection: Three</div>
</form>

You can also invert the hide/show behaviour (hide on checked) by setting the data-toggle-element-invert on the target elements to true, like so:

<form>
  <label>
    <input type='radio' name='radio-buttons' value='one' data-toggle-element='.radio-button-selections'>
    One
  </label>
  <label>
    <input type='radio' name='radio-buttons' value='two' data-toggle-element='.radio-button-selections'>
    Two
  </label>
  <label>
    <input type='radio' name='radio-buttons' value='three' data-toggle-element='.radio-button-selections'>
    Three
  </label>

  <div class='radio-button-selections' data-toggle-element-value='one' data-toggle-element-invert='true'>
    Not selected: One
  </div>
  <div class='radio-button-selections' data-toggle-element-value='two' data-toggle-element-invert='true'>
    Not selected: Two
  </div>
  <div class='radio-button-selections' data-toggle-element-value='three' data-toggle-element-invert='true'>
    Not selected: Three
  </div>
</form>

Select

Give a select tag a data-toggle-element of the selector that selects all the possible elements you wish to hide/show based on the value of the select. There should be one element per possible value of the select.

Give each of the elements that you wish to hide/show a data-toggle-element-value that corresponds to a possible value of the aforementioned select.

Add a data-toggle-element-value-none for an element to be shown when no selection is made.

Add a data-toggle-element-value-any for an element to be shown when any selection is made.

<form>
  <select data-toggle-element='.select-selections'>
    <option></option>
    <option value='one'>One</option>
    <option value='two'>Two</option>
    <option value='three'>Three</option>
  </select>

  <div class='select-selections' data-toggle-element-value-none='true'>No selection</div>
  <div class='select-selections' data-toggle-element-value-any='true'>Selection made</div>
  <div class='select-selections' data-toggle-element-value='one'>Selection: One</div>
  <div class='select-selections' data-toggle-element-value='two'>Selection: Two</div>
  <div class='select-selections' data-toggle-element-value='three'>Selection: Three</div>
</form>