Home / Admin / Property Type Conditional Search Fields
Duplicate Snippet

Embed Snippet on Your Site

Property Type Conditional Search Fields

sagar rane
<10
Code Preview
js
jQuery(document).ready(function($) {
    // ============================================
    // FIELD VISIBILITY BASED ON PROPERTY TYPE
    // ============================================
    // All conditional field labels (must match your "Label on Front End" exactly)
    var allConditionalFields = [
        'BHK Type',
        'Furnished Status', 
        'Status',
        'Min Area (sqft)',
        'Max Area (sqft)',
        'Land (Acres)',
        'Possession Date',
        'State'
    ];
    // Fields per property type
    var fieldMap = {
        'residential': ['BHK Type', 'Furnished Status', 'Status', 'Min Area (sqft)', 'Max Area (sqft)', 'Possession Date', 'State'],
        'commercial':  ['Status', 'Min Area (sqft)', 'Max Area (sqft)', 'Possession Date', 'State'],
        'land':        ['Land (Acres)', 'State'],
        'plot':        ['Land (Acres)', 'State'],
        'industrial':  ['Status', 'Min Area (sqft)', 'Max Area (sqft)', 'State'],
        'warehouse':   ['Status', 'Min Area (sqft)', 'Max Area (sqft)', 'State']
    };
    // ============================================
    // HELPER — find field wrapper by label text
    // ============================================
    function getFieldByLabel(labelText) {
        return $('.adv_search_field, .search_field_wrapper').filter(function() {
            return $(this).find('label, .search_label, span').text().trim() === labelText;
        });
    }
    // Hide all conditional fields
    function hideAllConditional() {
        $.each(allConditionalFields, function(i, label) {
            getFieldByLabel(label).hide();
        });
    }
    // Show specific fields
    function showFields(fieldsArray) {
        $.each(fieldsArray, function(i, label) {
            getFieldByLabel(label).show();
        });
    }
    // Show all fields (when no type selected)
    function showAllConditional() {
        $.each(allConditionalFields, function(i, label) {
            getFieldByLabel(label).show();
        });
    }
    // ============================================
    // MAIN FUNCTION — runs on type change
    // ============================================
    function updateFieldVisibility(selectedText) {
        var type = selectedText.toLowerCase().trim();
        if (!type || type === 'types' || type === 'select type' || type === 'all') {
            showAllConditional();
            return;
        }
        hideAllConditional();
        // Match property type keywords
        $.each(fieldMap, function(keyword, fields) {
            if (type.includes(keyword)) {
                showFields(fields);
                return false; // break loop
            }
        });
    }
    // ============================================
    // WATCH FOR TYPE DROPDOWN CHANGE
    // ============================================
    $(document).on('change', 'select[name="types"], select[name="type_search"], .adv_search_type select', function() {
        var selected = $(this).find('option:selected').text();
        updateFieldVisibility(selected);
    });
    // Run on page load
    setTimeout(function() {
        var initialType = $('select[name="types"], select[name="type_search"], .adv_search_type select').find('option:selected').text();
        updateFieldVisibility(initialType);
    }, 500);
});

Comments

Add a Comment