5

I have a link which posts some data to a controller. It is working fine and updates the data. But I want to change the css of the parent div but it is not happening. I have tried a lot of variations but I must be doing something stupid.

The code is below

View:

@foreach (var item in Model)
{
    <div class="added-property-excerpt">
        ...
        <div class="added-property-links">
        ...
        @if (!item.IsPropertyDisabled) { 
                @Html.ActionLink("Take off the Market" , "Disable", "Property", new { id = item.PropertyId }, new { id ="disable-link" })
            }
            else {
                @Html.ActionLink("Bring on the Market" , "Enable", "Property", new { id = item.PropertyId }, new { id ="enable-link" })
            }
        </div>
    </div>
}

JQuery

 <script>
     $(function () {
         $('a#disable-link').click(function (e) {
             $('.added-property-links').text('loading...');
             $.ajax({
                 url: this.href,
                 dataType: "text json",
                 type: "POST",
                 data: {},
                 success: function (data, textStatus) { }
             });
             $(this).closest('.added-property-excerpt').css("background", "red");
          // $(this).parent('.added-property-excerpt').css("background", "red");
             e.preventDefault();
         });
     });
 </script>
Tripping
  • 919
  • 4
  • 18
  • 36

2 Answers2

5

did you try:

$(this).parents('.added-property-excerpt').css("background", "red");

parents with an s.

What it does is that it goes from parent to parent until it find the class you want.

VVV
  • 7,563
  • 3
  • 34
  • 55
  • Good point. I've never used closest but I've now read the documentation. Thanks – VVV Oct 14 '12 at 00:23
4

Try this

$(function () {
    $('a#disable-link').click(function (e){
        var parentDiv=$(this).closest('.added-property-excerpt');
        $('.added-property-links').text('loading...');
        parentDiv.css("background", "red");
        $.ajax({...});
        e.preventDefault();
    });
});

See the difference in a working example and non-working example.

The Alpha
  • 143,660
  • 29
  • 287
  • 307