mercredi 11 septembre 2019

How to write If statement at jQuery for less code

I am writing the code below and I want to write less because functions duplicates. At the first case, when the selection of the element with id^=quform_10_218_ changes, some other elements be cleared. At the second case, when the button with class='.closebtn' is clicked, does almost the same thing like the firs case. I mean either the selection changes or the button is clicked, their functions do almost the same thing. So, how can I write an if-statement to cover all of these at once?

//first case
$(document).ready(function() {
$("[id^=quform_10_218_]").change(function(event) {
$("[id^=quform_10_1167_],[id^=quform_10_1150_],[id^=quform_10_1157_],[id^=quform_10_1155_],[id^=quform_10_1151_],[id^=quform_10_1350_],[id^=quform_10_2968_], [id^=quform_10_1145_]").find("option:selected").removeAttr("selected").val("").change();
$("[id^=quform_10_26_], [id^=quform_10_3361_], [id^=quform_10_237_],[id^=quform_10_57_]").val("");
});});});});
.....
...some code
.....
//second case
$(function ($) {
$('.closebtn').click(function () {
$("[id^=quform_10_218_], [id^=quform_10_1167_],[id^=quform_10_1150_],[id^=quform_10_1157_], [id^=quform_10_1155_],[id^=quform_10_1151_],[id^=quform_10_1350_], [id^=quform_10_2968_],[id^=quform_10_1145_]").find("option:selected").removeAttr("selected").val("").change();
$("[id^=quform_10_26_], [id^=quform_10_3361_], [id^=quform_10_237_],[id^=quform_10_57_]").val("");
});
$(this).closest('.quform-element-group').slideUp(800);
$("[id^=quform_10_92_]").attr("checked", false);
});});});

Aucun commentaire:

Enregistrer un commentaire