I have this example block of code that keeps repeating so many times now.
$(function(loadDatabyClickA){
$('button').live('click', function(){
selectedAgeType = $(this).attr('value');
var x = {};
x.data = $('a').attr("data"); //selected item in tree (.liselected)
x.command = $('a').attr("cmd");
x.option = "x";
x.sessionid = docCookies.getItem("sessionid");
x.ageType = selectedAgeType;
x.showData = showUnderlyingData;
var action = function(result, status) {
var x_list = "";
$.each(result, function(i, val){
x_list += "<li><h3>"+val.xtitle+"</h3></li>";
});
$('#x_view').append(x_list);
};
$.post("jsoncommand", JSON.stringify(chart), action)
.error(function(){
alert('error');
});
})
})
How can I extract this block so I can access it from another function? Maybe like using .extend()? This block is used by other function like this:
$(function(loadDatabyClickB){
$('button2').live('click', function(){
selectedAgeType = $(this).attr('value');
var y = {};
y.data = $('a').attr("data"); //selected item in tree (.liselected)
y.command = $('a').attr("cmd");
y.option = "y";
y.sessionid = docCookies.getItem("sessionid");
y.ageType = selectedAgeType;
y.showData = showUnderlyingData;
var action = function(result, status) {
var y_list = "";
$.each(result, function(i, val){
y_list += "<li><h3>"+val.ytitle+"</h3></li>";
});
$('#y_view').append(y_list);
};
$.post("jsoncommand", JSON.stringify(y), action)
.error(function(){
alert('error');
});
})
})
You guys can just show me a hint of how to do this because I don't have a clue. I will simplify it myself. Thanks in advance for your help!