I have this code on HTML
<ul id="menu">
<li class="mainmenu" id="newsarticles"><a href="#">News & articles</a>
<ul id="newsarticles">
<li class="submenu" id="news"> <a href="#">News</a>
</li>
<li class="submenu" id="articles"> <a href="#">Articles</a>
</li>
</ul>
<li class="mainmenu" id="contactus"><a href="#">Contact Us</a>
</li>
</ul>
and i call with jQuery ajax this code in order to create a new tree menu and call html pages, my problem is that i want to use the same function on these two menus, and i wonder if there is a way to do this without writing the same function two times:
$(document).ready(function() {
$("#menu li").on("click", function(e) {
e.stopPropagation();
var currentId = $(this).attr('id');
var scriptPath = currentId+".html";
var tree = $(this).closest("ul").attr("id");
$.ajax({
url: scriptPath,
type:'HEAD',
success: function()
{
//file exists
$("#tree").html($("#"+tree).html());
$("#text").load(scriptPath);
$("#tree li").click(Menu_callTabs); //the same function for tree menu
}
});
});
});
function Menu_callTabs(){
$("#menu li").on("click", function(e) {
var currentId = $(this).attr('id');
var scriptPath = currentId+".html";
var tree = $(this).closest("ul").attr("id");
$.ajax({
url: scriptPath,
type:'HEAD',
success: function()
{
//file exists
$("#tree").html($("#"+tree).html());
$("#text").load(scriptPath);
$("#tree li").click(Menu_callTabs); //the same function for tree menu
}
});
});
}
Any help appreciated.