Search code examples
javascriptphpjqueryyiiyii-components

CHtml Link returns null for parent


I created a url using Chtml::Link like this:

CHtml::link("Remove", '#', array('class' => 'delete')))

Now, when user clicks it i send a ajax request like this:

$('.delete').click(function(e) {
    e.preventDefault();
    $.ajax({
      url:'" . $this->createUrl('//shop/shoppingCart/delete') . "',
      type : 'GET',
      data: {id: $position},
      success: function(result) {
         console.log($(this).parent());
      },
     });
});

it always logs "undefined" but the link is inside a td What i want to do is remove the row containing the link. Any new approach is also welcome. Please help!


Solution

  • this inside the success function of the ajax call is another this than the this in the click function. So, you probably can't find the parent of that this.

    I guess you are trying to find the parent of the element to update the html right? Why don't you use Chtml::ajaxLink instead, then you can use the update property:

    Chtml::ajaxLink(
        'Remove',
        $this->createUrl('//shop/shoppingCart/delete'),
        array(
            'update' => "$('.delete').parent()", // or anything like this
            'data' => array(
                'id' => $position,
            )
        ),
        array(
            'class' => 'delete'
        )
    )