1

i am getting undefined when i try to pass attribute value (from button which opens a modal) to Java script function.

Component:

<aura:component>
.....
 <button type="button" data-id="test" onclick="{!c.DetailPage}" class="btn btn-warning btn-lg pull-right" data-toggle="modal" data-target="#myModal">Click</button>
....

</aura:component>

JS Controller:

 DetailPage : function(component, event, helper) {
// tried multilple scenarios..all returning "undefined"
        alert($(this).attr('data-id'));
        alert($(this).attr("data-id"));
        alert($(this).data('id'));
        alert($(this).data("id"));
        alert($(this).data().id);
    },

After retrieving this value i need to set this value to my drop down list..

<aura:attribute name="options" type="String[]"/>

<button type="button" data-id="{!v.options}"    ...../>
..
<ui:inputSelect aura:id="ProductPlatform" class="form-control" label="Product Platform" change="{!c.onSelectChange}" required="true"> 
    <aura:iteration items="{!v.pltfrmGrpOptions}" var="level" indexVar="index">
        <aura:if isTrue="{!index ==0}">
        <ui:inputSelectOption value="true" label="{!level}" text="{!level}"/>
        <aura:set attribute="else">
        <ui:inputSelectOption label="{!level}" text="{!level}" />
        </aura:set>
        </aura:if>
    </aura:iteration>
</ui:inputSelect>
1

you can get current button attributes from event object using following way

event.target.getAttribute("data-id")
  • Question Edited. This helps. But i need to set this data-id value to my drop down list. on the first attempts alert(event.target.getAttribute("data-id")) returns null, on second it gives correct value. – Anchal Jindal Nov 26 '15 at 9:49
  • i can use component.set() to set the value ...Coming to (event.target.getAttribute("data-id") but it returns null on first click.. Second click it gives value... but do not set the Drop down. – Anchal Jindal Nov 27 '15 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.