I have an asp.net codebehind page linking several checkboxes to javascript methods. I want to make only one javascript method to handle them all since they are the same logic, how would i do this?
code behind page load:
checkBoxShowPrices.Attributes.Add("onclick", "return checkBoxShowPrices_click(event);");
checkBoxShowInventory.Attributes.Add("onclick", "return checkBoxShowInventory_click(event);");
aspx page javascript; obviously they all do the same thing for their assigned checkbox, but i'm thinking this can be reduced to one method:
function checkBoxShowPrices_click(e)
{
if (_hasChanged)
{
confirm('All changes will be lost. Do you wish to continue?', function(arg)
{
if (arg.toUpperCase() == 'YES')
{
var checkBox = document.getElementById('<%=checkBoxShowPrices.UniqueID%
>');
checkBox.checked = !checkBox.checked;
eval("<%=base.GetPostBackEventReference(checkBoxShowPrices)%>");
_hasChanged = false;
}
});
return false;
}
else
{
eval("<%=base.GetPostBackEventReference(checkBoxShowPrices)%>");
}
}
function checkBoxShowInventory_click(e)
{
if (_hasChanged)
{
confirm('All changes will be lost. Do you wish to continue?', function(arg)
{
if (arg.toUpperCase() == 'YES')
{
var checkBox = document.getElementById('<%
=checkBoxShowInventory.UniqueID%>');
checkBox.checked = !checkBox.checked;
eval("<%=base.GetPostBackEventReference(checkBoxShowInventory)%>");
_hasChanged = false;
}
});
return false;
}
else
{
eval("<%=base.GetPostBackEventReference(checkBoxShowInventory)%>");
}
}