Question

I have a JSP page which uses an <s:select> tag.

<s:select 
        id="userGroups"
        headerKey="-1"
        headerValue="------ Select Group ------" list="userGroupList"
        listValue="groupName"
        onchange="selectGroup()">   

what i want to achieve is to display the listValue of the selected item using a javascript function. what may be the way to do this?

EDIT: my selectGroup() function:

function selectGroup(){
    var selectedGroup = document.getElementById('userGroups');
    alert(selectedGroup.value);
}
Was it helpful?

Solution

function selectGroup(){
    var sel = document.getElementById('userGroups');
    alert(sel.options[sel.selectedIndex].text);
}

or

<s:select 
    id="userGroups"
    headerKey="-1"
    headerValue="------ Select Group ------" list="userGroupList"
    listValue="groupName"
    onchange="selectGroup(this)"> 

function selectGroup(sel){
    alert(sel.options[sel.selectedIndex].text);
}

or

<s:select 
    id="userGroups"
    headerKey="-1"
    headerValue="------ Select Group ------" list="userGroupList"
    listValue="groupName"
    onchange="alert(this.options[this.selectedIndex].text);"> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top