Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a table in Angularjs that has a checkbox in each row. When the checkbox is clicked, I would like an alert function to be launched to display the content of the row being clicked. The problem I face is how can the alert function access the data content of the row?

The table in html looks like this;

<table class="table table-hover data-table sort display">
<thead>
  <tr>
    <th>Name</th>
    <th>Location</th>   
    <th>Checkbox Alert</th>
  </tr>
</thead>
<tbody>
  <tr ng-repeat="item in filteredList | orderBy:columnToOrder:reverse">  
    <td>{{item.name}}</td>
    <td>{{item.location}}</td>  
    <td> <input type="checkbox" ng-click="alert_display()"> </td>
  </tr>
</tbody>
</table>

The controller code looks like this;

$scope.alert_display = function()
{
    alert("Testing");
};

I would like alert_display() to display the contents of {{item.name}} and {{item.location}} of the relevant row.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Do the following:

<input type="checkbox" ng-model="selected" ng-change="display(selected, item)">

your JS code:

$scope.display = function(selected, item) {

    if(selected)
         alert(item.name + ' ' + item.location);
    else
         // do sth else

}

Here is the fiddle: http://jsfiddle.net/HB7LU/4156/

share|improve this answer
    
Thanks. It works partially. Upvoted but it is not exactly the answer I want. For some reason, I have to check, then uncheck the checkbox in order to launch the display function. How can I get the display function to launch everytime I click the checkbox, regardless of whether I am checking or unchecking it? –  user3293156 Jun 10 at 10:44
    
check the fiddle, now it alerts whenever you click | unclick –  anvarik Jun 10 at 10:50
    
You are the man! That's the answer I want! I have marked your answer as the right one. Thanks!! –  user3293156 Jun 10 at 10:51
    
no problem!! :) –  anvarik Jun 10 at 10:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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