Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery parents/closest not working

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>
like image 409
Tripping Avatar asked Oct 14 '12 00:10

Tripping


2 Answers

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.

like image 190
VVV Avatar answered Nov 13 '22 16:11

VVV


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.

like image 33
The Alpha Avatar answered Nov 13 '22 16:11

The Alpha