'this' in an attribute event handler refers to 'window', not the
element itself. You're better of using proper event listeners:

$('.thing a').click(function(){
   $(this).parents('.setting').remove();
});

or a function to which the event will be passed by default:

function removeSetting(event){
  $(event.target || event.srcElement).closest('.setting').remove();
};

<a href="#" onclick="removeSetting()">remove</a>

On 23 jan, 20:57, candlerb <b.cand...@pobox.com> wrote:
> I am trying to convert the following Prototype code to jQuery:
>
> <div class=".thing">
>   ...
>   <a href="#" onclick="$(this).up('.setting').remove()">remove</a>
> </div>
>
> Basically, when a user clicks on the 'remove' link I want the entire
> enclosing div which contains this link to vanish from the DOM. But
> there are multiple divs, each with their own 'remove' links. (*)
>
> With jQuery I have tried onclick="$(this).closest('.setting').remove
> ()" but it doesn't work - nothing happens. To debug I tried
> onclick="alert($(this).toSource())" and it showed me an empty object.
>
> I am sure I'm missing something obvious. When I google for this I just
> get examples where a click callback function is registered - $(this)
> is set appropriately within the callback. But here I just want to
> stick some Javascript inline in an onclick="..." handler.
>
> Many thanks,
>
> Brian.
>
> (*) Aside: original Prototype-based code taken 
> fromhttp://railscasts.com/episodes/75
> - I am migrating this app to jQuery using jRails.

Reply via email to