If inside a for loop doesn't work right

I learned Javascript from your web courses. Thank you for that. Now I am developing a web app and I have trouble with a javascript that doesn’t work as expected and I don’t understand why. Basically I have one dropdown with 3 options and I want to populate a second dropdown with these exact options and then select in the second dropdown the selected option in the first dropdown. This is the entire html / javascript code:

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">

    <script src="http://cdn.ckeditor.com/4.6.1/standard/ckeditor.js"></script>

    <select id="select_one">
      <option>Option 1</option>
      <option>Option 2</option>
      <option>Option 3</option>

    <button onclick="insertFunction()">Copy dropdown</button>

    <select id="select_two">

    function insertFunction(){
      var dd1 = document.getElementById("select_one");
      var dd2 = document.getElementById("select_two");
      var selectedOption = dd1.options[dd1.selectedIndex].text;

      var indexToSelect;

      for (i=0; i<dd1.length; i++){
        dd2.add(new Option(dd1.options[i].text,'value'));

          alert (dd1.options[i].text);

          if (dd1.options[i].text = selectedOption){
            alert (dd1.options[i].text + '---' + selectedOption);


As you can see, the alert dd1.option[i].text shows me the correct value. However, the following IF says that it is another value. This is what I don’t understand. Why there are 2 different value for the same thing?

I wrote these alerts because I wanted to show the results. In the end, I will take the selected option index from the first dropdown and use it to select the index in the second dropdown.

Pls help.

The if statement should trigger only once.

I am pretty sure you wanted to check an equality, instead of assigning a value, right? :slight_smile:

:)) Thank you. I didn’t realize that. It works now.