A powerful, lightweight JavaScript library for creating dynamic form dependencies with advanced logical operations. Control the visibility and availability of select options based on complex conditions using AND, OR, and parenthesis grouping.
- OR Logic (
|): Create conditions where any one of multiple criteria can be met - Parenthesis Grouping (
()): Build complex nested conditions with proper precedence - Enhanced Expression Evaluation: Smart parser handles complex logical expressions
- Backward Compatible: All existing v1.x implementations continue to work without modification
- Dynamic Filtering: Automatically shows/hides options based on parent element selections
- Multi-Parent Support: Options can depend on values from multiple parent elements
- Logical Operators:
&(AND) - All conditions must be true|(OR) - At least one condition must be true()(Grouping) - Control evaluation order
- Multiple Value Support: Match against multiple values using comma-separated lists
- Checkbox Support: Include checkbox states in your conditional logic
- Auto-Reset: Automatically clears invalid selections when options become unavailable
- Select2 Integration: Built-in support for Select2 enhanced dropdowns
- Zero Dependencies: Works with vanilla JavaScript (jQuery only needed for Select2)
- Lightweight: Minimal footprint with optimized performance
- Easy Integration: Simple API with data attributes
<!-- jQuery (required only if using Select2) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!-- Select Relations JS -->
<script src="https://deirvlon.github.io/select-relations/select-relations.js"></script>Download select-relations.js and include it in your project:
<script src="path/to/select-relations.js"></script><select id="country" class="select-relations">
<option value="">Select Country</option>
<option value="usa">USA</option>
<option value="canada">Canada</option>
</select>
<select id="city" class="select-relations" data-sf-parent="country">
<option value="">Select City</option>
<option value="ny" data-pr="country:usa">New York</option>
<option value="la" data-pr="country:usa">Los Angeles</option>
<option value="toronto" data-pr="country:canada">Toronto</option>
<option value="vancouver" data-pr="country:canada">Vancouver</option>
</select>Add this class to any select element that participates in the relation system (both parent and child elements).
Specify which select elements control this element. Use comma-separated IDs for multiple parents.
Define the condition that must be met for this option to be available.
<option data-pr="selectId:value">Option Text</option><option data-pr="selectId:value1,value2,value3">Option Text</option>All conditions must be true:
<option data-pr="country:usa&state:california">Option Text</option>At least one condition must be true:
<option data-pr="country:usa|country:canada">Option Text</option>Control evaluation order:
<option data-pr="(country:usa&state:ca)|(country:canada&province:on)">
Option Text
</option><option data-pr="status:active&(priority:high|priority:urgent)">
Option Text
</option><select id="product_type" class="select-relations">
<option value="">Select Product Type</option>
<option value="laptop">Laptop</option>
<option value="desktop">Desktop</option>
<option value="tablet">Tablet</option>
</select>
<select id="brand" class="select-relations" data-sf-parent="product_type">
<option value="">Select Brand</option>
<option value="apple" data-pr="product_type:laptop|product_type:tablet">Apple</option>
<option value="dell" data-pr="product_type:laptop|product_type:desktop">Dell</option>
<option value="hp" data-pr="product_type:laptop|product_type:desktop">HP</option>
<option value="samsung" data-pr="product_type:tablet">Samsung</option>
</select>
<select id="specs" data-sf-parent="product_type,brand">
<option value="">Select Specification</option>
<option value="m1" data-pr="product_type:laptop&brand:apple">MacBook M1</option>
<option value="m2" data-pr="product_type:laptop&brand:apple">MacBook M2</option>
<option value="xps13" data-pr="product_type:laptop&brand:dell">XPS 13</option>
<option value="ipad" data-pr="product_type:tablet&brand:apple">iPad Pro</option>
</select><select id="country" class="select-relations">
<option value="">Select Country</option>
<option value="usa">USA</option>
<option value="canada">Canada</option>
<option value="international">International</option>
</select>
<label>
<input type="checkbox" id="premium_member" class="select-relations">
Premium Member
</label>
<select id="shipping" data-sf-parent="country,premium_member">
<option value="">Select Shipping</option>
<option value="standard" data-pr="country:usa|country:canada">
Standard Shipping
</option>
<option value="express" data-pr="(country:usa|country:canada)&premium_member:1">
Express Shipping (Premium Members)
</option>
<option value="overnight" data-pr="country:usa&premium_member:1">
Overnight (US Premium Only)
</option>
<option value="international" data-pr="country:international|premium_member:1">
International (All Countries for Premium)
</option>
</select><select id="region" class="select-relations">
<option value="">Select Region</option>
<option value="north_america">North America</option>
<option value="europe">Europe</option>
</select>
<select id="country" class="select-relations" data-sf-parent="region">
<option value="">Select Country</option>
<option value="usa" data-pr="region:north_america">USA</option>
<option value="canada" data-pr="region:north_america">Canada</option>
<option value="uk" data-pr="region:europe">United Kingdom</option>
<option value="germany" data-pr="region:europe">Germany</option>
</select>
<select id="state" class="select-relations" data-sf-parent="country">
<option value="">Select State/Province</option>
<option value="ca" data-pr="country:usa">California</option>
<option value="ny" data-pr="country:usa">New York</option>
<option value="on" data-pr="country:canada">Ontario</option>
<option value="bc" data-pr="country:canada">British Columbia</option>
</select>
<select id="city" class="select-relations" data-sf-parent="state">
<option value="">Select City</option>
<option value="la" data-pr="state:ca">Los Angeles</option>
<option value="sf" data-pr="state:ca">San Francisco</option>
<option value="nyc" data-pr="state:ny">New York City</option>
<option value="buffalo" data-pr="state:ny">Buffalo</option>
<option value="toronto" data-pr="state:on">Toronto</option>
<option value="vancouver" data-pr="state:bc">Vancouver</option>
</select><select id="category" class="select-relations">
<option value="1" data-alt="electronics">Electronics</option>
<option value="2" data-alt="clothing">Clothing</option>
</select>
<select id="product" data-sf-parent="category">
<!-- Match by value -->
<option data-pr="category:1">Laptop</option>
<!-- Match by data-alt -->
<option data-pr="category:electronics">Phone</option>
</select>If you're dynamically adding elements after page load:
// Manually trigger re-initialization
window.updateSelectRelations();<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
<script>
$(document).ready(function() {
$('.select-relations').select2({
placeholder: "Select an option",
allowClear: true
});
});
</script>You can also use relations on other form elements:
<input type="checkbox" id="agree" class="select-relations">
<div id="terms_content" data-sf-parent="agree" data-pr="agree:1">
<!-- This div will show/hide based on checkbox -->
Terms and conditions content...
</div>The library follows standard logical operator precedence:
- Parentheses
()- Highest priority (evaluated first) - AND
&- Medium priority - OR
|- Lowest priority
A&B|C β (A AND B) OR C
A|B&C β A OR (B AND C)
(A|B)&C β (A OR B) AND C
A&(B|C) β A AND (B OR C)
(A&B)|(C&D) β (A AND B) OR (C AND D)
Enable console logging to see what's happening:
// Add this before loading select-relations.js
window.SELECT_RELATIONS_DEBUG = true;- Minimize DOM Queries: Use IDs for parent selects for faster lookups
- Simplify Conditions: Break complex conditions into multiple steps when possible
- Limit Depth: Avoid deeply nested cascading selects (>5 levels)
- Batch Updates: When programmatically updating multiple selects, use
window.updateSelectRelations()once at the end
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
- IE11 (with polyfills)
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Version 2.0.0 is fully backward compatible with v1.x. All existing code will continue to work without modification.
To use the new features, simply update your data-pr attributes:
Before (v1.x):
<option data-pr="country:usa&state:ca">California Option</option>After (v2.0.0) - Same syntax works, plus new options:
<!-- OR Logic -->
<option data-pr="country:usa|country:canada">North America Option</option>
<!-- Grouping -->
<option data-pr="(country:usa&state:ca)|(country:canada&province:on)">
Complex Option
</option>Select Relations JS is released under the MIT License. See LICENSE file for details.
Author: Kamran Gasimov
Company: Deirvlon Technologies
Website: https://deirvlon.com
Created: April 9, 2024
Last Updated: October 8, 2025
Β© All rights are reserved Deirvlon Technologies.
- Examples: https://deirvlon.github.io/select-relations/select_relations_demo.html
- Documentation: https://deirvlon.github.io/select-relations
- Issues: GitHub Issues
- Email: support@deirvlon.com
If you find this library helpful, please consider giving it a star on GitHub!
Made with β€οΈ by Deirvlon Technologies