Assign an existing click event function to another click event using jquery

Posted by Peter Delahunty on Stack Overflow See other posts from Stack Overflow or by Peter Delahunty
Published on 2009-10-27T05:38:41Z Indexed on 2010/04/17 12:43 UTC
Read the original article Hit count: 286

Filed under:
|

Ok so i have some html like this:

<div id="navigation">

<ul>

<li>
<a>tab name</a>
<span class="delete-tab">X</span>
</li>

<li>
<a>tab name</a>
<span class="delete-tab">X</span>
</li>

<li>
<a>tab name</a>
<span class="delete-tab">X</span>
</li>

<li class="selected">
<a>tab name</a>
<span class="tab-del-btn">X</span>
</li>

</ul>

</div>

I then have javascript that is excuted on the page that i do not control (this is in liferay portal). I want to then manipulate things afterwards with my own custom javascript.

SO...

For each of the span.delete-tab elements an on-click event function has been assign earlier. It is the same function call for each span. I want to take that function (any) and call it from the click event of the span.tab-del-btn ?

This is what i tried to do:

var navigation = jQuery('#navigation');
var navTabs = navigation.find('.delete-tab');

var existingDeleteFunction = null;

navTabs.each(function (i){
        var tab = jQuery(this);
        existingDeleteFunction = tab.click;
});

var selectedTab = jQuery('#navigation li.selected');
var deleteBtn = selectedTab.find('.tab-del-btn');

deleteBtn.click(function(event){
        existingDeleteFunction.call(this);
});

It does not work though. existingDeleteFunction is not the original function it is some jquery default function.

Any ideas?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about events