I have made a function that if i check a checkbox an text will be added to an textarea.
I have several checkboxes, so i dont want the textarea to get empty when i uncheck any of the checkboxes. my code works, but the problem is the code is really awfully coded and makes loading time too long since this is for a mobile site i need to shorten it, any help is appreciated.
$(document).ready(function () {
$('#checkbox-6').change(function () {
if ($(this).is(':checked')) {
$('#append').append(' Till - Hiss finnes ')
} else {
var thiss = $('#append');
thiss.html(thiss.html().replace(/ Till - Hiss finnes /ig, ""));
}
});
$('#checkbox-7').change(function () {
if ($(this).is(':checked')) {
$('#append').append(' Behöver hjälp med uppackning ')
} else {
var upack = $('#append');
upack.html(upack.html().replace(/ Behöver hjälp med uppackning /ig, ""));
}
});
$('#ch1').change(function () {
if ($(this).is(':checked')) {
$('#append').append(' Från - Hiss finnes ')
} else {
var fhiss = $('#append');
fhiss.html(fhiss.html().replace(/ Från - Hiss finnes /ig, ""));
}
});
$('#ch2').change(function () {
if ($(this).is(':checked')) {
$('#append').append(' Packning ')
} else {
var pack = $('#append');
pack.html(pack.html().replace(/ Packning /ig, ""));
}
});
$('#ch3').change(function () {
if ($(this).is(':checked')) {
$('#append').append(' Vind Källare Förråd ')
} else {
var attic = $('#append');
attic.html(attic.html().replace(/ Vind Källare Förråd /ig, ""));
}
});
$('#ch4').change(function () {
if ($(this).is(':checked')) {
$('#append').append(' Flyttstädning ')
} else {
cleaning = $('#append');
cleaning.html(cleaning.html().replace(/ Flyttstädning /ig, ""));
}
});
$('#ch5').change(function () {
if ($(this).is(':checked')) {
$('#append').append(' Behöver flyttkartonger ')
} else {
var boxes = $('#append');
boxes.html(boxes.html().replace(/ Behöver flyttkartonger /ig, ""));
}
});
});
Thanks!
You can try this.