Hi there,
You could do something like this:
$(':checkbox').click(
function() {
var idPart = this.id.split('_')[1];
if ($(this).is(":checked")){
$('#tbl_' +
idPart).show('medium');
}
else{
$('#tbl_' +
idPart).hide('medium');
}
}
);
One thing you might want to do is add some context to the ":checkbox"
selector because it might be a little slow on its own.
You could make this even shorter by using a ternary operator if you
want:
$(':checkbox').click(
function() {
var idPart = this.id.split('_')[1];
$('#tbl_' + idPart)[$(this).is(":checked") ? 'show' : 'hide']
('medium');
}
);
Hope that helps.
--Karl
_________________
Karl Swedberg
www.englishrules.com
www.learningjquery.com
On Feb 11, 2008, at 3:11 PM, rsmolkin wrote:
Hi,
I hope someone can help me out a bit.
I am finding myself doing a lot of code (as I'll paste below). It's
basically the same block of code with different variables.
What I am trying to do is consolidate all that code into a funciton
where I can declare all the checkboxes I need to create an Click
function for, and then loop through them and generate the click
handler as well as other functions for all of them.
Here is the code snippet:
$('#ckbx_wgmpo').click(
function() {
if ($(this).attr("checked") == true){
$('table#tbl_wgmpo').show('medium');
}
else{
$('table#tbl_wgmpo').hide('medium');
}
}
);
$('#ckbx_wgmse').click(
function() {
if ($(this).attr("checked") == true){
$('table#tbl_wgmse').show('medium');
}
else{
$('table#tbl_wgmse').hide('medium');
}
}
);
$('#ckbx_wgmoe').click(
function() {
if ($(this).attr("checked") == true){
$('table#tbl_wgmoe').show('medium');
}
else{
$('table#tbl_wgmoe').hide('medium');
}
}
);
$('#ckbx_tyd').click(
function() {
if ($(this).attr("checked") == true){
$('div#tyd_info').show('medium');
}
else{
$('div#tyd_info').hide('medium');
}
}
);
$('#ckbx_tydpo').click(
function() {
if ($(this).attr("checked") == true){
$('table#tbl_tydpo').show('medium');
}
else{
$('table#tbl_tydpo').hide('medium');
}
}
);
$('#ckbx_tydgc').click(
function() {
if ($(this).attr("checked") == true){
$('table#tbl_tydgc').show('medium');
}
else{
$('table#tbl_tydgc').hide('medium');
}
}
);
$('#ckbx_tydoe').click(
function() {
if ($(this).attr("checked") == true){
$('table#tbl_tydoe').show('medium');
}
else{
$('table#tbl_tydoe').hide('medium');
}
}
);