Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.6 kB
1
Indexable
Never
$(document).ready(function () {
    scopeRadios = $('input[name="scope"]');
    categorySelect = $('#category');
    sourceSelect = $('#source');
    unitInput = $('#unit');
    unitInput2 = $('#unit2').hide(); // hide second input box by default
    carbonFactorInput = $('#carbonfactor');
    resultInput = $('#result');
    commentInput = $('#comment');
    charCount = $('#charCount');

    // trigger change event on page load to initialize state
    resetForm();
});

function resetForm() {
    // Clear inputs and textarea
    $('input').val('');
    $('textarea').val('');
    $('#unit-label2').text('');
    $('#charCount').text('0/250');
    unitInput2.hide();
    unitInput.removeClass('col-4').addClass('col-8');
    $('.invalid-input').removeClass('invalid-input');

    // Clear radio button selection
    scopeRadios.parent().removeClass('active');
    scopeRadios.prop('checked', false);

    // Reset category and source select elements
    categorySelect.empty().append(new Option("Välj kategori", "", true, true)).prop('disabled', true);
    sourceSelect.empty().append(new Option("Välj källa", "", true, true)).prop('disabled', true);
}

scopeRadios.change(function () {
    resetForm();
    categorySelect.prop('disabled', false);
    let selectedScope = $(this).val();
    let categories = [...new Set(data.filter(item => item.scope == selectedScope).map(item => item.category))];
    categories.forEach(category => {
        categorySelect.append(new Option(category, category));
    });
});

$('#clear').click(function() {
    resetForm();
});