Skip to content

wpic/show-by-value

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Simple jquery plugin to show and hide inputs in the form by value of the other inputs in the form.

(function ( $ ) {

    $.fn.showByValue = function( options ) {

        return this.each(function(e) {
            var form = $(this);

            var apply_shows = function() {
                $('[data-show-by-value]', form).each(function() {
                    var list = $(this).attr('data-show-by-value').split(',');
                    for (l in list) {
                        var parts = list[l].split('=');

                        var selector = "[name='" + parts[0] + "']";
                        if (parts.length > 1) {
                            if ($(selector, form).val() === parts[1]) {
                                $(this).show();
                                return;
                            }
                        }
                        else if ($(selector, form).val()) {
                            $(this).show();
                            return;
                        }
                    }

                    $(this).hide();
                });
            }

            // handle shows
            $('select[name], input[name], textarea[name]', form).change(apply_shows);
            apply_shows();
        });
    }

} ( jQuery ));

Sample

<form>
    <select name="mode">
        <option>None</option>
        <option value="first">First</option>
        <option value="secound">Second</option>
    </select>
    <input data-show-by-value="mode=first" value="This is the first one" />
    <input data-show-by-value="mode=secound" value="This is the second one" />
    <input data-show-by-value="mode=first,mode=secound" value="One or two" />

    <br/>
    <input name="typing" type="text" value="clear to hide other one" />
    <input data-show-by-value="typing=" type="text" value="it's empty" />
    <input data-show-by-value="typing" type="text" value="it's not empty" />
</form>

<script>
$('form').showByValue();
</script>

About

Simple jquery plugin to show and hide inputs in the form by value of the other inputs in the form.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published