Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!