I have some JS code that when the user clicks the "add" button, some text is displayed to a textarea.
The code seems to be working OK. However, I realize I must refactor the code so that I am using only the one on click
function instead of the six that I have for each of the 6 add buttons.
$('#row_split_id_cover_letter_details_first_paragraph').prepend('<div id="id_standard_suggestion_01" class="cover_letter_suggestion_content margin-bottom-15"><span id="id_1a1" class="margin-bottom-15">Standard Sentence Number 1.1. </span><span class="cover_letter_suggestion_add_button"><button id="id_add_standard_suggestion_paragragh01_01" class="btn btn-xs rounded btn-primary" type="button">{% trans "Add" %}</button></span></div><div id="id_standard_suggestion_02" class="cover_letter_suggestion_content margin-bottom-15"><span id="id_1a2" class="margin-bottom-15">Standard Sentence Number 1.2. </span><span class="cover_letter_suggestion_add_button"><button id="id_add_standard_suggestion_paragragh01_02" class="btn btn-xs rounded btn-primary" type="button">{% trans "Add" %}</button></span></div><div id="id_standard_suggestion_03" class="cover_letter_suggestion_content margin-bottom-15"><span id="id_1a3" class="margin-bottom-15">Standard Sentence Number 1.3. </span><span class="cover_letter_suggestion_add_button"><button id="id_add_standard_suggestion_paragragh01_03" class="btn btn-xs rounded btn-primary" type="button">{% trans "Add" %}</button></span></div><div id="id_standard_suggestion_04" class="cover_letter_suggestion_content margin-bottom-15"><span id="id_1a4" class="margin-bottom-15">Standard Sentence Number 1.4. </span><span class="cover_letter_suggestion_add_button"><button id="id_add_standard_suggestion_paragragh01_04" class="btn btn-xs rounded btn-primary" type="button">{% trans "Add" %}</button></span></div><div id="id_standard_suggestion_05" class="cover_letter_suggestion_content margin-bottom-15"><span id="id_1a5" class="margin-bottom-15">Standard Sentence Number 1.5. </span><span class="cover_letter_suggestion_add_button"><button id="id_add_standard_suggestion_paragragh01_05" class="btn btn-xs rounded btn-primary" type="button">{% trans "Add" %}</button></span></div><div id="id_standard_suggestion_06" class="cover_letter_suggestion_content margin-bottom-15"><span id="id_1a6" class="margin-bottom-15">Standard Sentence Number 1.6. </span><span class="cover_letter_suggestion_add_button"><button id="id_add_standard_suggestion_paragragh01_06" class="btn btn-xs rounded btn-primary" type="button">{% trans "Add" %}</button></span></div>');
$(function () {
$('#id_add_standard_suggestion_paragragh01_01').on('click', function () {
var divTA1 = document.getElementById('id_cover_letter_details_first_paragraph');
var divGS1 = document.getElementById('id_1a1');
divTA1.innerHTML = divTA1.innerHTML + divGS1.innerHTML;
});
$('#id_add_standard_suggestion_paragragh01_02').on('click', function () {
var divTA1 = document.getElementById('id_cover_letter_details_first_paragraph');
var divGS1 = document.getElementById('id_1a2');
divTA1.innerHTML = divTA1.innerHTML + divGS1.innerHTML;
});
$('#id_add_standard_suggestion_paragragh01_03').on('click', function () {
var divTA1 = document.getElementById('id_cover_letter_details_first_paragraph');
var divGS1 = document.getElementById('id_1a3');
divTA1.innerHTML = divTA1.innerHTML + divGS1.innerHTML;
});
$('#id_add_standard_suggestion_paragragh01_04').on('click', function () {
var divTA1 = document.getElementById('id_cover_letter_details_first_paragraph');
var divGS1 = document.getElementById('id_1a4');
divTA1.innerHTML = divTA1.innerHTML + divGS1.innerHTML;
});
$('#id_add_standard_suggestion_paragragh01_05').on('click', function () {
var divTA1 = document.getElementById('id_cover_letter_details_first_paragraph');
var divGS1 = document.getElementById('id_1a5');
divTA1.innerHTML = divTA1.innerHTML + divGS1.innerHTML;
});
$('#id_add_standard_suggestion_paragragh01_06').on('click', function () {
var divTA1 = document.getElementById('id_cover_letter_details_first_paragraph');
var divGS1 = document.getElementById('id_1a6');
divTA1.innerHTML = divTA1.innerHTML + divGS1.innerHTML;
});
});
Here is a working fiddle.