Code Monkey home page Code Monkey logo

conditional-script's Introduction

conditionalScript

conditionalScript is a simple native JavaScript ES6 script which lets you conditionally show/hide Form or any other elements by using custom HTML5 attributes. conditionalScript currently works with checkboxes, radios and select lists.

This script does not requires jQuery though, it should work on any modern browser, If you have an issue/idea, please feel free to fork the repo or send me a push request and i'll be more than happy to push it.

Usage

  1. Place the script before the closing Body tag for better page speed performance.
<script src="js/conditional-script.min.js"></script>
  1. Create 2 divs with the same class name, you can name them whatever you like.
<div class="conditional-script">
    Main Markup here...
</div>
<div class="conditional-script">
    Conditional Markup here...
</div>
  1. Add additional data-conditional-name and data-conditional-value attributes to your newly created (Conditional Markup). Please note that the data-conditional-name attribute will target the Field Name attribute, so you need to set the Name attribute to the field in order to work properly.
<div class="conditional-script">
    <input type="..." name="exampleName" value="main field value">
</div>
<div class="conditional-script">
    Conditional Markup .. data-conditional-name="exampleName" data-conditional-value="main field value"
</div>

Also, data-conditional-value accepts more than 1 value but they must be separated by comma followed by space (", ") for example:

data-conditional-value="value 1, value 3"

You final markup should look like this:

<!-- Main Div - Radio buttons Bootstrap markup example -->
<div class="form-group">
    <div class="form-check">
        <input class="form-check-input" type="radio" name="exampleRadioName" id="exampleRadio1" value="option1">
        <label class="form-check-label" for="exampleRadio1">yes</label>
    </div>
    <div class="form-check">
        <input class="form-check-input" type="radio" name="exampleRadioName" id="exampleRadio2" value="option2">
        <label class="form-check-label" for="exampleRadio2">no</label>
    </div>
</div>
<!-- Conditional Div -->
<div class="form-group">
  <label for="formGroupExampleInput">Show on radio check with value Yes and hide if No</label>
  <input type="text" class="form-control" id="exampleRadio" data-conditional-name="exampleRadioName" data-conditional-value="option1">
</div>

conditional-script's People

Contributors

awran5 avatar

Stargazers

 avatar

Watchers

 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.