jquery parents/closest not working

Tripping picture Tripping · Oct 14, 2012 · Viewed 13k times · Source

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>

Answer

VVV picture VVV · Oct 14, 2012

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.