I am trying to pass an iteration variable into my client side controller (I know there are a number of posts on this but haven't found any that work).
I've found some resources online that set iteration variables as a div id… for example:
<aura:iteration items="{!v.newCases}" var="case">
<button type="button" onclick="{!c.showCaseDeleteModal}" id={!case.Id}>Delete</button>
</aura:iteration>
found at https://developer.salesforce.com/forums/?id=906F0000000kAn0IAE
However, this hasn't worked. Here is my code(iteration var is in quotes… both did not worked):
aura:
<aura:iteration items="{!v.car_list}" var="car" indexVar="i">
<div id="{!car.id}" class="expand-btn" onclick="{!c.expand}">
<lightning:icon iconName="utility:threedots" size="x-small" alternativeText="expand" />
</div>
</aura:iteration>
js:
expand: function(component, event, helper){
var id = event.target.id;
console.log('car_id', id);
var div = document.getElementById('foo');
if (div.style.display !== 'none') {
div.style.display = 'none';
}
else {
div.style.display = 'block';
}
}
})
The only thing logged is 'car_id'.
Best Answer
I figured out the issue. The above logic would work fine , however, using lightning:icon caused event.target to point to all of the stuff under the hood of this aura feature.
So in order to get around this I had to traverse up the dom to find the id that I wanted to access. Here is my updated code:
aura:
expand.js: