Access data in foreach loop

Hi Everyone,

I want to access the values and I am using a loop as I am creating a list but I am not able to retrive the value of a particular id when I click on it. However, using helpers, I am able to display the right values but not able to display in alert when I click on it.

Here is what I have already did.

  • Creating collection is sucessful

  • display in text
    for this I created a div and used helpers and used

`{{#each}}
<button id="---">{{post_id}}</button
{{/each}}

I am able to print the post id but I want when I click on button, the right value which is getting displayed on the button gets printed on the alert.

I tried using JQuery but getting the only first value I dont know why, May be I need to send an argument with the helper.

Please help me. Its very urgent

Please post your full template (html and js) as there just isn’t enough context here for anyone to help you, I think.

1 Like

Thanks @vigorwebsolutions, I have done that, here ist the logic I implemented,

I have given class to my button and a post id assigned as id of the button and on capture click on the button I used this

here is the code



'click .custom1':function(){
		// var postId = JSON.stringify(postId);
		// var buttontext=$(".custom").text(); 
	var str = JSON.stringify(this);	
	 var id = JSON.parse(str);
 		var c = PostLikes.find({user_id:Session.get("userId"),post_id:id.post_id,"like_status":1}).fetch();
		var buttontext=id; 
 		if(c[0]){
		if(c[0].like_status == 0){
		buttontext =  "Like";
		}else{
		buttontext = "Unlike";			
		}
		}else{
			buttontext = "Like";
		}
			
			var status = "";
			if(buttontext == "Like"){
			    status=1;
			}else{
			    status=0;
		}
		Meteor.call('like_a_post', id.post_id, Session.get("userId"),status,function(error, result){
		if(error){
			alert('Error');
               }else{
		if(status==0){
	         // alert('Unlike');
			}else{
			// alert('like');
	}
}});	
}