Hello,
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">
<head>
<meta charset="utf-8">
<title></title>
<script src="http://cdn.ckeditor.com/4.6.1/standard/ckeditor.js"></script>
</head>
<body>
<select id="select_one">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<button onclick="insertFunction()">Copy dropdown</button>
<select id="select_two">
</select>
<script>
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);
}
}
}
</script>
</body>
</html>
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.