Kolla
Kolla

Reputation: 75

How do I use javascript to select onchange from a drop down and place it in an innerHTML?

This select calls usrTidrapport and gets a list of users from there that it then puts in a dropdownlist or combobox maybe is the right term.

What I'm trying to do is to capture the name from this list in an onchange and put it in an innerHTML. I for some reason can't get the text value of the name, I can only seem to get the value which is "AnvId".

(usrTidrapport is a com+ component so this is VB6 but I think the example is general enough to apply to most languages and it's mainly the javascript that is the problem for me, or so I think.)

<td>
        <span title="visar vem som (utför) arbetsordern och dess uppgifter i ITS.">
            Ansvarig Utförare
            <SELECT id="cboAnsvarigUt" name="cboAnsvarigUt" style="width: 150px;" onfocus="focsel()" onblur="unfocsel()" tabindex="40" onchange="changeHandler()">
            <OPTION value=-1></OPTION>
            <%usrTidrapport.HamtaAnvandareDropdown "", 0, true, AnvId%>
            <%usrTidrapport.HamtaAnvandareDropdown "", 0, false, AnvId%>
        </SELECT>
        </span>

    </td>

This is where I want to update the innerHTML after a value from the dropdown is selected.

<td><span name="spnAnsvarigUt" id="spnAnsvarigUt"></span></td>

This is a piece of javascript that actually works, it will pick out the value, whis is AnvId, and put it in the innerHTML for spnAnsvarigUt which is where I want it. I however want the text value, not the "value" whis is AnvId.

<script type="text/javascript">
    function changeHandler() {

        document.getElementById('spnAnsvarigUt').innerHTML = ThisForm.cboAnsvarigUt.value;
    }
</script>

Upvotes: 0

Views: 618

Answers (1)

Harry
Harry

Reputation: 89790

This would get you the content (.innerHTML) present within the selected <option> tag.

function changeHandler() {
   var selOption = ThisForm.cboAnsvarigUt.selectedIndex;
   var selValue = ThisForm.cboAnsvarigUt.options[selOption].innerHTML;
   document.getElementById('spnAnsvarigUt').innerHTML = selValue;
}

Demo Fiddle

Upvotes: 2

Related Questions