Getting the text from a drop-down box

后端 未结 12 2186
无人及你
无人及你 2020-11-30 02:23

This gets the value of whatever is selected in my dropdown menu.

document.getElementById(\'newSkill\').value

I cannot however find out what

相关标签:
12条回答
  • 2020-11-30 02:40

    Here is an easy and short method

    document.getElementById('elementID').selectedOptions[0].innerHTML
    
    0 讨论(0)
  • 2020-11-30 02:41

    This should return the text value of the selected value

    var vSkill = document.getElementById('newSkill');
    
    var vSkillText = vSkill.options[vSkill.selectedIndex].innerHTML;
    
    alert(vSkillText);
    

    Props: @Tanerax for reading the question, knowing what was asked and answering it before others figured it out.

    Edit: DownModed, cause I actually read a question fully, and answered it, sad world it is.

    0 讨论(0)
  • 2020-11-30 02:41
    var selectoption = document.getElementById("dropdown");
    var optionText = selectoption.options[selectoption.selectedIndex].text;
    
    0 讨论(0)
  • 2020-11-30 02:42

    Please try the below this is the easiest way and it works perfectly

    var newSkill_Text = document.getElementById("newSkill")[document.getElementById("newSkill").selectedIndex];
    
    0 讨论(0)
  • 2020-11-30 02:47

    Based on your example HTML code, here's one way to get the displayed text of the currently selected option:

    var skillsSelect = document.getElementById("newSkill");
    var selectedText = skillsSelect.options[skillsSelect.selectedIndex].text;
    
    0 讨论(0)
  • 2020-11-30 02:48
        var ele = document.getElementById('newSkill')
        ele.onchange = function(){
                var length = ele.children.length
                for(var i=0; i<length;i++){
                    if(ele.children[i].selected){alert(ele.children[i].text)};              
                }
        }   
    
    0 讨论(0)
提交回复
热议问题