\$\begingroup\$
\$\endgroup\$
2
I'm kinda new at this jQuery stuff and I've gathered up this much to make my code functional, but I would really want to shorten it. I've already tried $('#Monday','#Tuesday'....) to group them, but with no success.
$('#Monday').click(function() {
if ($(this).hasClass("active")){
$(this).switchClass( "active", "inactive", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', true);
} else {
$(this).switchClass( "inactive", "active", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', false);
}
$(this).find('input').prop('checked', function( foo, oldValue ) {return !oldValue});
});
$('#Tuesday').click(function() {
if ($(this).hasClass("active")){
$(this).switchClass( "active", "inactive", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', true);
} else {
$(this).switchClass( "inactive", "active", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', false);
}
$(this).find('input').prop('checked', function( foo, oldValue ) {return !oldValue});
});
$('#Wednesday').click(function() {
if ($(this).hasClass("active")){
$(this).switchClass( "active", "inactive", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', true);
} else {
$(this).switchClass( "inactive", "active", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', false);
}
$(this).find('input').prop('checked', function( foo, oldValue ) {return !oldValue});
});
$('#Thursday').click(function() {
if ($(this).hasClass("active")){
$(this).switchClass( "active", "inactive", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', true);
} else {
$(this).switchClass( "inactive", "active", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', false);
}
$(this).find('input').prop('checked', function( foo, oldValue ) {return !oldValue});
});
$('#Friday').click(function() {
if ($(this).hasClass("active")){
$(this).switchClass( "active", "inactive", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', true);
} else {
$(this).switchClass( "inactive", "active", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', false);
}
$(this).find('input').prop('checked', function( foo, oldValue ) {return !oldValue});
});
$('#Saturday').click(function() {
if ($(this).hasClass("active")){
$(this).switchClass( "active", "inactive", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', true);
} else {
$(this).switchClass( "inactive", "active", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', false);
}
$(this).find('input').prop('checked', function( foo, oldValue ) {return !oldValue});
});
$('#Sunday').click(function() {
if ($(this).hasClass("active")){
$(this).switchClass( "active", "inactive", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', true);
} else {
$(this).switchClass( "inactive", "active", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', false);
}
$(this).find('input').prop('checked', function( foo, oldValue ) {return !oldValue});
});
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
1 Answer 1
\$\begingroup\$
\$\endgroup\$
You should add a class to all elements, something like .day, and then use that class in your code:
$('.day').click(function() {
if ($(this).hasClass("active")){
$(this).switchClass( "active", "inactive", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', true);
} else {
$(this).switchClass( "inactive", "active", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', false);
}
$(this).find('input').prop('checked', function( foo, oldValue ) {return !oldValue});
});
Another way would be:
$('#Monday, #Tuesday, #Wendnesday').click(function() {
if ($(this).hasClass("active")){
$(this).switchClass( "active", "inactive", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', true);
} else {
$(this).switchClass( "inactive", "active", 200, "easeInOutQuad" );
$(this).parent().find('.miniTime').prop('disabled', false);
}
$(this).find('input').prop('checked', function( foo, oldValue ) {return !oldValue});
});
default
click()
calls have indentical contents, why not just have ONE$('#Sunday, #Monday, #Tuesday, #blahblahbhal').click(...)
call? \$\endgroup\$$('#Monday','#Tuesday'....)
to group them, but that has a syntax error. There should only be one set of single quotes around the whole thing like Marc B suggests. \$\endgroup\$