So I have a dropdown box with values "No" and "Yes" but it will not property display it when selected. It just comes up as blank. No matter what I choose, I want it to display what I have chosen, either "Yes" or "No".
So I got the class names for everything that I need.
For the selected value that is being displayed in the dropdown box is:
<a class="select2-choice" tabindex="-1" onclick="return false;" href="javascript:void(0)">
<span class="select2-chosen">No</span>
<abbr class="select2-search-choice-close"></abbr>
.....
</a>
The dropdown box html code is:
<ul class="select2-results">
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">No</div>
</li>
<li class="select2-results-dept-0 select2-result select2-result-selectable select2-highlighted">
<div class="select2-result-label">Yes</div>
</li>
</ul>
So I want to access the values of select2-result-label and set them to the select2-chosen content value. I tried doing it, but it's giving me an error in FireBug.
Here's what I did so far... What am I doing wrong?
function testFunc() {
var x = document.getElementsByClassName("select2-chosen");
var y = document.getElementsByClassName("select2-results-dept-0 select2-result select2-result-selectable select2-highlighted")
.getElementsByClassName("select2-result-label");
x[0].innerHTML = y;
}
You are using
getElementsByClassName
in the wrong context. This function merely returns all elements that have any of the specified class names.As
select2-highlighted
is only assigned to a selected element, it's the only class you should care about. Making these changes should help: