-2

I saw an example here, but not cler where the elementid is being passed from?

Can someone please guide?

(Lightning) How do I get the selected value from a Select dropdown list & send it to a method?

Here's the code snippet

I saw an example here, but not clear where the elementid is being passed from?

Can someone please guide?

(Lightning) How do I get the selected value from a Select dropdown list & send it to a method?

Here's my code snippet. I am trying to get the selected picklist value to be passed to my apex controller

// component

<aura:component 
            controller="BuildPicklistOfUsers" 
            implements="flexipage:availableForRecordHome,force:hasRecordId" 
            access="global" >

<aura:handler name="init" value="{!this}" action="{!c.doInit} / >
<aura:attribute name="users" type="user[]"></aura:attribute>
<aura:attribute name="myselecteduser" type="String" access="global"/>

<lightning:select label="Choose User" name="uzr" aura:id="uzr" onChange="{! c.onChangeUser }"> 
    <aura:iteration items="{!v.users}" var="user">
        <option value="{!user.Id}">{!user.Name}</option>
    </aura:iteration>
</lightning:select>

// client side controller ({ doInit: function(component, event, helper) {

var action = component.get("c.getUserList");

action.setCallback(this, function(result){
var users = result.getReturnValue();

component.set("v.users", users);
window.setTimeout(
$A.getCallback( function() {
component.find("uzr").set("v.value", users[4].Id);
}));
});
$A.enqueueAction(action);
},

onChangeUser: function (component, event, helper) {

var selectedUser = component.get("v.uzr");
var action = component.get("c.getSelectedUserId");
action.setParams({
    "selectedUser": userid
});
action.setCallback(this, function(response) {
    this.getResponse(response, component);
});
$A.enqueueAction(action);

},

getResponse: function(response, component) {
var stringResult = response.getReturnValue();
component.set("v.myselecteduser", stringResult);
}

})

1
  • Could you please update your post with a minimal reproduceable sample of YOUR code, and highlight exactly where you are stuck? thanks.
    – glls
    Commented Oct 22, 2018 at 21:45

1 Answer 1

1

To get the values from a normal select list, you can do something like this:

var selected =
    Array.prototype.filter.call(
        component.find("selectList").getElement().options, 
        function(option) { 
            return option.selected; 
        }
    ).map(
        function(option) {
            return option.value;
        }
    );

Where "selectList" is the select list:

<select aura:id="selectList">
    <aura:iteration items="{!v.options}" var="option">
        <option value="{!option.value}">{!option.label}</option>
    </aura:iteration>
</select>
1
  • Thankyou @sfdcfox
    – user4947
    Commented Oct 23, 2018 at 0:54

You must log in to answer this question.