GithubHelp home page GithubHelp logo

fieldspy's Introduction

Fieldspy

Fieldspy lets you show or hide fields based on other fields current value.

Features

  • Easy to setup
  • Only requires jQuery
  • Can handle multiple conditions at once
  • Use on unlimited fields
  • No CSS to include

Enqueue Scripts

<script src="jquery.js"></script>
<script src="jquery.fieldspy.min.js"></script>

jQuery

jQuery.fn.fieldspy( condition, callback );

The jQuery fieldspy function accepts 2 parameters and is applied to the field you would like to show or hide based on the condition given to the condition parameter. The callback parameter lets you pass a function that handles the result of the condition. Have a look at the following example:

var condition = [
    { '.other-field-selector' : 'value-to-look-for' }
];
$('.field-selector').fieldspy( condition, function(result) {
    $(this).toggle(result);
});

In the example above, the result parameter of the callback function is a boolean and $('.field-selector') will show or hide if value-to-look-for is the current value of .other-field-selector. The condition must always be an array containing object literals.

Writing Conditions

Fieldspy is capable of handling multiple "and/or" conditions simultaneously. However, Fieldspy currently only works for "choice" field types (i.e. select, radio & checkbox). You may have already noticed that in the example above the condition variable is an array that holds an object literal. You can pass multiple object literals to the condition array and than simply imagine an "or" operator between each object and an "and" operator between all name-value pairs. Have a look at the example below:

var condition = [
    {
        '.the-checkbox' : [ 'one', 'three' ],
        // and
        '.the-checkbox-2' : 'number',
    },
    // or
    {
        '.the-select' : 'adidas',
        //and
        '.the-select-2' : 'shoe',
    },
    // or
    {
        '.the-radio' : [ 'green', 'blue' ],
        // and
        '.the-radio-2' : 'color',
    }
];

Value arrays (i.e. [ 'one', 'three' ] & [ 'green', 'blue' ]) inside an object are shorthand "and" operators. With that in mind, the example above would translate to an "if" statement that would look like this:

if (
    ( $('.the-checkbox').val() === 'one' && $('.the-checkbox').val() === 'three' && $('.the-checkbox-2').val() === 'number' )
    ||
    ( $('.the-select').val() === 'adidas' && $('.the-select-2').val() === 'shoe' )
    ||
    ( $('.the-radio').val() === 'green' && $('.the-radio').val() === 'blue' && $('.the-radio-2').val() === 'color' )
) {
    // do stuff
}

JSON Translations

Because Fieldspy deals with object literals in it's conditions, JSON can help translate conditions that are written in other languages. Here is an example written in PHP:

$condition = array(
    array(
        '.the-checkbox' => array('one','three'),
        // and
        '.the-checkbox-2' => 'number',
    ),
    // or
    array(
        '.the-select' => 'adidas',
        // and
        '.the-select-2' => 'shoe',
    ),
    // or
    array(
        '.the-radio' => array('green','blue'),
        // and
        '.the-radio-2' => 'color',
    )
);

$condition = json_encode( $condition );

?>
<script>
$('.field-selector').fieldspy( <?php echo $condition; ?>, function(result) {
    if ( result ) {
        $(this).show();
    } else {
        $(this).hide();
    }
});
</script>
<?php

CSS

Fieldspy does not handle any CSS on your behalf. However, there is no need to hide your fields initially via CSS yourself. When the DOM is ready, Fieldspy will fire once to set the inital state of your fields via the callbacks you chose to apply.

Roadmap

  • Passing conditions via HTML5 data-fieldspy-condition attribute.
  • Passign animation callbacks as keywords via HTML5 data-fieldspy-callback attribute.
  • Ability to check text & number input values.
  • Ability to pass a single object literal as a condition.
  • Creation of an HTML document showing examples.

fieldspy's People

Contributors

marcwiest avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.