I have this below code where i am trying to first search an account name and on hit button it displays the result set which is currently working for me.

Now i have an tag on the records displayed. On click of the record i need to write in debug console.

Can anyone let me know how i can do it. In the below code i am trying , but i am not getting it.

Component -

<aura:component controller="SimpleAccountController">
    <aura:attribute name="account" type="account[]"/>
    <ui:outputText aura:id="outName" value="" class="text"/>
    <ui:inputText aura:id="name" label="Enter Name:" placeholder="Your Name" />
    <ui:button aura:id="button" buttonTitle="Click to see what you put into the field" class="button" label="Click me" press="{!c.getInput}"/>
    <aura:iteration var="acc" items="{!v.account}">
            <a onclick="{!c.accountselected}" data-recId="{!acc.Id}">
        <p>{!acc.Name} : {!acc.Type} : {!acc.Industry} : {!acc.Rating}</p>

Controller -

    getInput : function(cmp, evt) {
        var myName = cmp.find("name").get("v.value");
        var myText = cmp.find("outName");
        var greet = "Hi, " + myName;
        myText.set("v.value", greet);
        var action = cmp.get("c.seaAcc");
        action.setParams({ "s" : myName });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set("v.account", response.getReturnValue());
                //alert("From server: " + response.getReturnValue());

    accountselected : function(cmp, evt) {
       console.log(evt.target.getAttribute("data-recId")) ;


event.currentTarget => points to the element which registered and called the click event's function (i.e ). So doing event.currentTarget.getAttribute("data-recId") will return the required id.

Please use event.currentTarget.getAttribute("data-recId").

| improve this answer | |

You can do it like this

Iteration in Component:

<aura:iteration var="acc" items="{!v.account}">
                <a onclick="{!c.accountselected}" data-value="{!acc.Id}">
            <p>{!acc.Name} : {!acc.Type} : {!acc.Industry} : {!acc.Rating}</p>


accountselected : function(cmp, evt) {
       var ctarget = event.currentTarget;
        var id_str = ctarget.dataset.value;
| improve this answer | |
  • So just for my knowledge i want to know.. We can create a child component and achieve this above scenario also... – user2542953 Oct 25 '17 at 11:26
  • If you want to pass the selected account id to child component then yes you can do it – salesforce Developer Oct 25 '17 at 11:29

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.