Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
53
rated 0 times [  57] [ 4]  / answers: 1 / hits: 15253  / 13 Years ago, tue, december 20, 2011, 12:00:00

I want to loop over a Dropdownlist and write the value of the selected Item in a label.
I've done it before with a selection of Radio buttons but with the dropdownlist it won't work.
Now, some Code.



Here is the generated HTML Code Values are not interesting.



<select id=alternativeNumbers name=alternativeNumbers>
<option value=1_A>Text</option>
<option value=2_B>Text</option>
<option value=3_C>Text</option>
<option value=4_D>Text</option>
<option value=5_E>Text</option>
<option value=6_F>Text</option>
</select>


The Code to bind the event to the Dropdownlist.



$(function () {
var dropdown = document.getElementsByName(alternativeNumbers);
$(dropdown ).change(function () {
updateAlternativeDropdown();
});
});


And finally the method which is called by the event. This should fill the labels.



function updateAlternativeDropdown() {
var dropdown = document.getElementsByName(alternativeNumbers);
var lengthDropDown = addAlternativeArticleNumberDropdown.length;
for (var i=0; i < lengthDropDown; i++)
{
//This alert is for the behavior of the output!
alert(addAlternativeArticleNumberDropdown[i].value);
if (addAlternativeArticleNumberDropdown[i].selected) {
var valueOfDropdown = addAlternativeArticleNumberDropdown[i].value;
var splittedValues = valueOfDropdown.split(_);
document.getElementById(label1).innerText = splittedValues[0];
document.getElementById(label2).innerText = splittedValues[1];
}
}
};


I hope this is enough information, now the Problem / Current behavior:



The method updateAlternativeDropdown() is called fine but then the alert inside the loop returns the value of first element, value of the selected element and this 3 times. (I guess because of the 6 elements in this element)



Furthermore because of this my if-statement isn't entered. Currently I#m kind of clueless where this problem comes from.



Thanks in advance.


More From » jquery

 Answers
6

You don't have to iterate dropdown's options. You can access selected option like this:



dropdown.options[dropdown.selectedIndex].value


Update your updateAlternativeDropdown function:





function updateAlternativeDropdown() {
var dropdown = document.getElementById(alternativeNumbers),
splittedValues = dropdown.options[dropdown.selectedIndex].value.split('_');
document.getElementById(label1).innerHTML = splittedValues[0];
document.getElementById(label2).innerHTML = splittedValues[1];
}

$('#alternativeNumbers').change(updateAlternativeDropdown);

<script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script>
label1: <span id=label1>-</span><br />
label2: <span id=label2>-</span><br />
<select id=alternativeNumbers name=alternativeNumbers>
<option value=1_A>Text</option>
<option value=2_B>Text</option>
<option value=3_C>Text</option>
<option value=4_D>Text</option>
<option value=5_E>Text</option>
<option value=6_F>Text</option>
</select>





working Example


[#88459] Monday, December 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arthur

Total Points: 729
Total Questions: 107
Total Answers: 109

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;