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

183
Visualizações
Selecting Dropdown option, change h1 with javascript

I am very new to coding and couldn't find solution with if condition for this.

I know how I can do it with html code and options, but this time I need to make it with arrays and if function.

Basically I just need a dropdown with languages (which I made) and then when I click on specific language (for example, English) - I need to change html h1 to "Hello!", when I click Latvian "Labdien" etc.

Basically I need to write a proper if function, hope you could tell me what's wrong there.

const select = document.getElementById("select"),
  arr = ["Latvian", "English", "Russian"];

for (var i = 0; i < arr.length; i++) {
  var option = document.createElement("OPTION"),
    txt = document.createTextNode(arr[i]);
  option.appendChild(txt);
  option.setAttribute("value", arr[i]);
  select.insertBefore(option, select.lastChild);
}

if (arr[0] = "Latvian") {
  document.getElementById("heading").innerHTML = "Labdien!";
} else if (arr[1] == "English") {
  document.getElementById("heading").innerHTML = "Hello!";
} else if (arr[2] == "Russian") {
  document.getElementById("heading").innerHTML = "Добрый день!";
}
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>JavaScript Dropdown</title>

  <link rel="stylesheet" href="./style.css">
</head>

<body>
  <h1 class="heading" id="heading"></h1>
  <select class="drop" id="select"></select>

  <script src="./main.js"></script>
</body>

Still figuring out whats wrong with if statement, there is some problem in :

if(arr[0] == "Latvian")
else if(arr[1] == "English")
else if(arr[2] == "Russian").

Or maybe I need to call a function and then place it onchoice in HTML? Help.. been googling and youtubing all day

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can add a change event listener to the select element. Also, you can use the index of each language as the value of the select options, and use the same array when set the h1 inner HTML:

var select = document.getElementById("select");
var arr = [
    { id: 1, language: 'Latvian', title: 'Labdien!' },
    { id: 2, language: 'English', title: 'Hello!' },
    { id: 3, language: 'Russian', title: 'Добрый день!' }
];

for (var i = 0; i < arr.length; i++) {
    var option = document.createElement("OPTION");
    var txt = document.createTextNode(arr[i].language);
    
    option.appendChild(txt);
    option.setAttribute('value', arr[i].id);
    select.insertBefore(option, select.lastChild);
}

// add a change event listener to handle the language title
select.addEventListener('change', changeHeading);

// trigger a change event in order to display the selected language's title
select.dispatchEvent(new Event('change'));

function changeHeading(event) {
    var language = arr.find(
        (language) => language.id === parseInt(event.target.value)
    );

    document.getElementById('heading').innerHTML = language.title;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Dropdown</title>

    <link rel="stylesheet" href="./style.css">
</head>

<body>
    <h1 class="heading" id="heading"></h1>
    <select class="drop" id="select"></select>

    <script src="./main.js"></script>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório
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