Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
How to use a selector to filter from a dataset?

I'm trying to filter values from a dataset and have it reflect on a page, using JavaScript, but am struggling to get the solution.

I need it so that when I select one of the values in the select filter, the narrative will also update accordingly to the correct value (the third column) in the data.

I think I'm having trouble partly because of the way this data will be structured in these arrays (see countryData in the JS. Though optimally that's how I think it comes to me when I pull it from my data source... Will prefer to keep it like that unless anyone sees major red flags.

Extra points if you can provide separate solutions using both vanilla JS and jquery!

HTML

<div class="tab-pane fade show active" id="mapTab">
    <div class="sidebar">
        <div class="select">
            <select id="countrySelector" class="form-select selectpicker" data-width="100%" data-size="8" name="country"
                data-dropdown>
                <option value="">Country</option>
                <option id="all" value="All">All</option>
                <option id="afg" value="Afghanistan">Afghanistan</option>
                <option value="Bangladesh">Bangladesh</option>
                <option value="Bhutan">Bhutan</option>
            </select>
        </div>
    </div>
</div>
<div id="narrative"></div>    
</div>

JavaScript

<script>
    const countryData = new Array([
    ["AFG", "Afghanistan", "Test narrative about Afghanistan"],
    ["BGD", "Bangladesh", "Test narrative about Bangladesh"],
    ["BTN", "Bhutan", "Test narrative about Bhutan"],
    ["IND", "India", "Test narrative about India"]
    ]
    )
    
    // Trying with vanilla JS
    const items = document.getElementById('countrySelector')

    let clickedItem = ''
    
    items.addEventListener('change', function (item) {
        console.log('You selected: ', this.value);
    displayNarrative(this.value)
    });
    
    function displayNarrative(countryKey) {
        document.getElementById("narrative").innerHTML =
        countryData.filter(function (countryRef) {
            return countryRef == countryKey;
        })
    }

    // Trying with Jquery
    $(document).ready(function () {
        var narrative = $('.narrative');

    $('select#countrySelector').change(function () {
        narrative.columns(2).search($(this).val()).draw();
        });
    })
</script>
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda