have a rethink and DRY your code
Nov 28, 2022
2
Level 1
jQuery .on('change', function() not working
Hi to all,
The problem is that I have some dynamically created sets of input tags and I also have a function that is meant to trigger any time an input value is changed.
I am writing query on change in multiple function. Some function Work perfect. But some function not work.
How to write correct code or this code correct or wrong
<script type="text/javascript">
$(function() {
$(document).live('change', function(event) {
if($("#one_hour_slots1").is(":checked") || $("#one_hour_slots2").is(":checked") || $("#one_hour_slots3").is(":checked")) {
$("#three_hour_slots7").prop('disabled', true);
$("#six_hour_slots1").prop('disabled', true);
$("#twelve_hour_slots1").prop('disabled', true);
} else {
$("#three_hour_slots7").prop('disabled', false);
$("#six_hour_slots1").prop('disabled', false);
$("#twelve_hour_slots1").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#one_hour_slots4").is(":checked") || $("#one_hour_slots5").is(":checked") || $("#one_hour_slots6").is(":checked")) {
$("#three_hour_slots8").prop('disabled', true);
$("#six_hour_slots1").prop('disabled', true);
$("#twelve_hour_slots1").prop('disabled', true);
} else {
$("#three_hour_slots8").prop('disabled', false);
$("#six_hour_slots1").prop('disabled', false);
$("#twelve_hour_slots1").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#one_hour_slots7").is(":checked") || $("#one_hour_slots8").is(":checked") || $("#one_hour_slots9").is(":checked")) {
$("#three_hour_slots1").prop('disabled', true);
$("#six_hour_slots2").prop('disabled', true);
$("#twelve_hour_slots1").prop('disabled', true);
} else {
$("#three_hour_slots1").prop('disabled', false);
$("#six_hour_slots2").prop('disabled', false);
$("#twelve_hour_slots1").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#one_hour_slots10").is(":checked") || $("#one_hour_slots11").is(":checked") || $("#one_hour_slots12").is(":checked")) {
$("#three_hour_slots2").prop('disabled', true);
$("#six_hour_slots2").prop('disabled', true);
$("#twelve_hour_slots1").prop('disabled', true);
} else {
$("#three_hour_slots2").prop('disabled', false);
$("#six_hour_slots2").prop('disabled', false);
$("#twelve_hour_slots1").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#one_hour_slots13").is(":checked") || $("#one_hour_slots14").is(":checked") || $("#one_hour_slots15").is(":checked")) {
$("#three_hour_slots3").prop('disabled', true);
$("#six_hour_slots3").prop('disabled', true);
$("#twelve_hour_slots2").prop('disabled', true);
} else {
$("#three_hour_slots3").prop('disabled', false);
$("#six_hour_slots3").prop('disabled', false);
$("#twelve_hour_slots2").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#one_hour_slots16").is(":checked") || $("#one_hour_slots17").is(":checked") || $("#one_hour_slots18").is(":checked")) {
$("#three_hour_slots4").prop('disabled', true);
$("#six_hour_slots3").prop('disabled', true);
$("#twelve_hour_slots2").prop('disabled', true);
} else {
$("#three_hour_slots4").prop('disabled', false);
$("#six_hour_slots3").prop('disabled', false);
$("#twelve_hour_slots2").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#one_hour_slots19").is(":checked") || $("#one_hour_slots20").is(":checked") || $("#one_hour_slots21").is(":checked")) {
$("#three_hour_slots5").prop('disabled', true);
$("#six_hour_slots4").prop('disabled', true);
$("#twelve_hour_slots2").prop('disabled', true);
} else {
$("#three_hour_slots5").prop('disabled', false);
$("#six_hour_slots4").prop('disabled', false);
$("#twelve_hour_slots2").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#one_hour_slots22").is(":checked") || $("#one_hour_slots23").is(":checked")) {
$("#three_hour_slots6").prop('disabled', true);
$("#six_hour_slots4").prop('disabled', true);
$("#twelve_hour_slots2").prop('disabled', true);
} else {
$("#three_hour_slots6").prop('disabled', false);
$("#six_hour_slots4").prop('disabled', false);
$("#twelve_hour_slots2").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots7").is(":checked")) {
$("#one_hour_slots1").prop('disabled', true);
$("#one_hour_slots2").prop('disabled', true);
$("#one_hour_slots3").prop('disabled', true);
} else {
$("#one_hour_slots1").prop('disabled', false);
$("#one_hour_slots2").prop('disabled', false);
$("#one_hour_slots3").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots8").is(":checked")) {
$("#one_hour_slots4").prop('disabled', true);
$("#one_hour_slots5").prop('disabled', true);
$("#one_hour_slots6").prop('disabled', true);
} else {
$("#one_hour_slots4").prop('disabled', false);
$("#one_hour_slots5").prop('disabled', false);
$("#one_hour_slots6").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots7").is(":checked") || $("#three_hour_slots8").is(":checked")) {
$("#six_hour_slots1").prop('disabled', true);
$("#twelve_hour_slots1").prop('disabled', true);
} else {
$("#six_hour_slots1").prop('disabled', false);
$("#twelve_hour_slots1").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots1").is(":checked")) {
$("#one_hour_slots7").prop('disabled', true);
$("#one_hour_slots8").prop('disabled', true);
$("#one_hour_slots9").prop('disabled', true);
} else {
$("#one_hour_slots7").prop('disabled', false);
$("#one_hour_slots8").prop('disabled', false);
$("#one_hour_slots9").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots2").is(":checked")) {
$("#one_hour_slots10").prop('disabled', true);
$("#one_hour_slots11").prop('disabled', true);
$("#one_hour_slots12").prop('disabled', true);
} else {
$("#one_hour_slots10").prop('disabled', false);
$("#one_hour_slots11").prop('disabled', false);
$("#one_hour_slots12").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots1").is(":checked") || $("#three_hour_slots2").is(":checked")) {
$("#six_hour_slots2").prop('disabled', true);
$("#twelve_hour_slots1").prop('disabled', true);
} else {
$("#six_hour_slots2").prop('disabled', false);
$("#twelve_hour_slots1").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots3").is(":checked")) {
$("#one_hour_slots13").prop('disabled', true);
$("#one_hour_slots14").prop('disabled', true);
$("#one_hour_slots15").prop('disabled', true);
} else {
$("#one_hour_slots13").prop('disabled', false);
$("#one_hour_slots14").prop('disabled', false);
$("#one_hour_slots15").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots4").is(":checked")) {
$("#one_hour_slots16").prop('disabled', true);
$("#one_hour_slots17").prop('disabled', true);
$("#one_hour_slots18").prop('disabled', true);
} else {
$("#one_hour_slots16").prop('disabled', false);
$("#one_hour_slots17").prop('disabled', false);
$("#one_hour_slots18").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots3").is(":checked") || $("#three_hour_slots4").is(":checked")) {
$("#six_hour_slots3").prop('disabled', true);
$("#twelve_hour_slots2").prop('disabled', true);
} else {
$("#six_hour_slots3").prop('disabled', false);
$("#twelve_hour_slots2").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots5").is(":checked")) {
$("#one_hour_slots19").prop('disabled', true);
$("#one_hour_slots20").prop('disabled', true);
$("#one_hour_slots21").prop('disabled', true);
} else {
$("#one_hour_slots19").prop('disabled', false);
$("#one_hour_slots20").prop('disabled', false);
$("#one_hour_slots21").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots6").is(":checked")) {
$("#one_hour_slots22").prop('disabled', true);
$("#one_hour_slots23").prop('disabled', true);
} else {
$("#one_hour_slots22").prop('disabled', false);
$("#one_hour_slots23").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#three_hour_slots5").is(":checked") || $("#three_hour_slots6").is(":checked")) {
$("#six_hour_slots4").prop('disabled', true);
$("#twelve_hour_slots2").prop('disabled', true);
} else {
$("#six_hour_slots4").prop('disabled', false);
$("#twelve_hour_slots2").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#six_hour_slots1").is(":checked")) {
$("#one_hour_slots1").prop('disabled', true);
$("#one_hour_slots2").prop('disabled', true);
$("#one_hour_slots3").prop('disabled', true);
$("#one_hour_slots4").prop('disabled', true);
$("#one_hour_slots5").prop('disabled', true);
$("#one_hour_slots6").prop('disabled', true);
$("#three_hour_slots7").prop('disabled', true);
$("#three_hour_slots8").prop('disabled', true);
} else {
$("#one_hour_slots1").prop('disabled', false);
$("#one_hour_slots2").prop('disabled', false);
$("#one_hour_slots3").prop('disabled', false);
$("#one_hour_slots4").prop('disabled', false);
$("#one_hour_slots5").prop('disabled', false);
$("#one_hour_slots6").prop('disabled', false);
$("#three_hour_slots7").prop('disabled', false);
$("#three_hour_slots8").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#six_hour_slots2").is(":checked")) {
$("#one_hour_slots7").prop('disabled', true);
$("#one_hour_slots8").prop('disabled', true);
$("#one_hour_slots9").prop('disabled', true);
$("#one_hour_slots10").prop('disabled', true);
$("#one_hour_slots11").prop('disabled', true);
$("#one_hour_slots12").prop('disabled', true);
$("#three_hour_slots1").prop('disabled', true);
$("#three_hour_slots2").prop('disabled', true);
} else {
$("#one_hour_slots7").prop('disabled', false);
$("#one_hour_slots8").prop('disabled', false);
$("#one_hour_slots9").prop('disabled', false);
$("#one_hour_slots10").prop('disabled', false);
$("#one_hour_slots11").prop('disabled', false);
$("#one_hour_slots12").prop('disabled', false);
$("#three_hour_slots1").prop('disabled', false);
$("#three_hour_slots2").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#six_hour_slots1").is(":checked") || $("#six_hour_slots2").is(":checked")) {
$("#twelve_hour_slots1").prop('disabled', true);
} else {
$("#twelve_hour_slots1").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#six_hour_slots3").is(":checked")) {
$("#one_hour_slots13").prop('disabled', true);
$("#one_hour_slots14").prop('disabled', true);
$("#one_hour_slots15").prop('disabled', true);
$("#one_hour_slots16").prop('disabled', true);
$("#one_hour_slots17").prop('disabled', true);
$("#one_hour_slots18").prop('disabled', true);
$("#three_hour_slots3").prop('disabled', true);
$("#three_hour_slots4").prop('disabled', true);
} else {
$("#one_hour_slots13").prop('disabled', false);
$("#one_hour_slots14").prop('disabled', false);
$("#one_hour_slots15").prop('disabled', false);
$("#one_hour_slots16").prop('disabled', false);
$("#one_hour_slots17").prop('disabled', false);
$("#one_hour_slots18").prop('disabled', false);
$("#three_hour_slots3").prop('disabled', false);
$("#three_hour_slots4").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#six_hour_slots4").is(":checked")) {
$("#one_hour_slots19").prop('disabled', true);
$("#one_hour_slots20").prop('disabled', true);
$("#one_hour_slots21").prop('disabled', true);
$("#one_hour_slots22").prop('disabled', true);
$("#one_hour_slots23").prop('disabled', true);
$("#three_hour_slots5").prop('disabled', true);
$("#three_hour_slots6").prop('disabled', true);
} else {
$("#one_hour_slots19").prop('disabled', false);
$("#one_hour_slots20").prop('disabled', false);
$("#one_hour_slots21").prop('disabled', false);
$("#one_hour_slots22").prop('disabled', false);
$("#one_hour_slots23").prop('disabled', false);
$("#three_hour_slots5").prop('disabled', false);
$("#three_hour_slots6").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#six_hour_slots3").is(":checked") || $("#six_hour_slots4").is(":checked")) {
$("#twelve_hour_slots2").prop('disabled', true);
} else {
$("#twelve_hour_slots2").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#twelve_hour_slots1").is(":checked") ) {
$("#one_hour_slots1").prop('disabled', true);
$("#one_hour_slots2").prop('disabled', true);
$("#one_hour_slots3").prop('disabled', true);
$("#one_hour_slots4").prop('disabled', true);
$("#one_hour_slots5").prop('disabled', true);
$("#one_hour_slots6").prop('disabled', true);
$("#one_hour_slots7").prop('disabled', true);
$("#one_hour_slots8").prop('disabled', true);
$("#one_hour_slots9").prop('disabled', true);
$("#one_hour_slots10").prop('disabled', true);
$("#one_hour_slots11").prop('disabled', true);
$("#one_hour_slots12").prop('disabled', true);
$("#three_hour_slots7").prop('disabled', true);
$("#three_hour_slots8").prop('disabled', true);
$("#three_hour_slots1").prop('disabled', true);
$("#three_hour_slots2").prop('disabled', true);
$("#six_hour_slots1").prop('disabled', true);
$("#six_hour_slots2").prop('disabled', true);
} else {
$("#one_hour_slots1").prop('disabled', false);
$("#one_hour_slots2").prop('disabled', false);
$("#one_hour_slots3").prop('disabled', false);
$("#one_hour_slots4").prop('disabled', false);
$("#one_hour_slots5").prop('disabled', false);
$("#one_hour_slots6").prop('disabled', false);
$("#one_hour_slots7").prop('disabled', false);
$("#one_hour_slots8").prop('disabled', false);
$("#one_hour_slots9").prop('disabled', false);
$("#one_hour_slots10").prop('disabled', false);
$("#one_hour_slots11").prop('disabled', false);
$("#one_hour_slots12").prop('disabled', false);
$("#three_hour_slots7").prop('disabled', false);
$("#three_hour_slots8").prop('disabled', false);
$("#three_hour_slots1").prop('disabled', false);
$("#three_hour_slots2").prop('disabled', false);
$("#six_hour_slots1").prop('disabled', false);
$("#six_hour_slots2").prop('disabled', false);
}
});
});
$(function() {
$(document).on('change', function(event) {
if($("#twelve_hour_slots2").is(":checked")) {
$("#one_hour_slots13").prop('disabled', true);
$("#one_hour_slots14").prop('disabled', true);
$("#one_hour_slots15").prop('disabled', true);
$("#one_hour_slots16").prop('disabled', true);
$("#one_hour_slots17").prop('disabled', true);
$("#one_hour_slots18").prop('disabled', true);
$("#one_hour_slots19").prop('disabled', true);
$("#one_hour_slots20").prop('disabled', true);
$("#one_hour_slots21").prop('disabled', true);
$("#one_hour_slots22").prop('disabled', true);
$("#one_hour_slots23").prop('disabled', true);
$("#three_hour_slots3").prop('disabled', true);
$("#three_hour_slots4").prop('disabled', true);
$("#three_hour_slots5").prop('disabled', true);
$("#three_hour_slots6").prop('disabled', true);
$("#six_hour_slots3").prop('disabled', true);
$("#six_hour_slots4").prop('disabled', true);
} else {
$("#one_hour_slots13").prop('disabled', false);
$("#one_hour_slots14").prop('disabled', false);
$("#one_hour_slots15").prop('disabled', false);
$("#one_hour_slots16").prop('disabled', false);
$("#one_hour_slots17").prop('disabled', false);
$("#one_hour_slots18").prop('disabled', false);
$("#one_hour_slots19").prop('disabled', false);
$("#one_hour_slots20").prop('disabled', false);
$("#one_hour_slots21").prop('disabled', false);
$("#one_hour_slots22").prop('disabled', false);
$("#one_hour_slots23").prop('disabled', false);
$("#three_hour_slots3").prop('disabled', false);
$("#three_hour_slots4").prop('disabled', false);
$("#three_hour_slots5").prop('disabled', false);
$("#three_hour_slots6").prop('disabled', false);
$("#six_hour_slots3").prop('disabled', false);
$("#six_hour_slots4").prop('disabled', false);
}
});
});
</script>
How to change function in write correct way.
Please or to participate in this conversation.