window.onload=function load_data(){
document.getElementById('Fieldset_arrays').innerHTML='';
document.getElementById('Form_interface').innerHTML='';
var selected_array_item;
var new_string;
var new_user_array;
var obst = 'Obst';
const Obst = ["Apfel", "Birne", "Orange", "Erdbeere", "Banane"];
var gemüse = 'Gemüse';
const Gemüse = ["Gurke", "Aubergine", "Zuccini", "Möhre", "Tomate"];
const Arrays = new Array();
Arrays [0] = obst;
Arrays [1] = Obst;
Arrays [2] = gemüse;
Arrays [3] = Gemüse;
//var array_chosen;
var parent_arrays = document.getElementById('Fieldset_arrays');
for (let i = 0; i < Arrays.length; i = i + 2)
{
ul = document.createElement('ul');
ul.id = 'Ul_' + Arrays[i];
ul.style = 'display:inline-block;';
parent_arrays.appendChild(ul);
h4 = document.createElement('h4');
h4.textContent = Arrays[i];
ul.appendChild(h4);
var li_array = Arrays[i + 1];
for (let i2 = 0; i2 < li_array.length; i2++)
{
li = document.createElement('li');
li.id = 'Li_' + li_array[i2];
li.innerHTML = li_array[i2];
ul.appendChild(li);
}
}
var parent_interface;
parent_interface = document.getElementById('Form_interface');
label_select_array = document.createElement('label');
label_select_array.innerHTML = 'was möchten sie tun?';
label_select_array.style = 'margin-right:24px;';
parent_interface.appendChild(label_select_array);
/*select_array = document.createElement('select');
select_array.id = 'Select_array';
parent_interface.appendChild(select_array);
br_select_array1 = document.createElement('br');
parent_interface.appendChild(br_select_array1);
br_select_array2 = document.createElement('br');
parent_interface.appendChild(br_select_array2);
for (let i = 0; i < Arrays.length; i = i + 2)
{
opt_array_choice = document.createElement('option');
opt_array_choice.id = 'Opt_'+Arrays[i];
opt_array_choice.value = Arrays[i];
opt_array_choice.innerHTML = Arrays[i];
select_array.appendChild(opt_array_choice);
}
document.getElementById('Select_array').addEventListener('change', array_choice);
function array_choice()
{
array_chosen = document.getElementById('Select_array').value;
}*/
btn_change = document.createElement('button');
btn_change.id = 'Btn_change';
btn_change.innerHTML = 'Ändern';
btn_change.style = 'margin-right:12px;';
btn_change.type = 'button';
parent_interface.appendChild(btn_change);
document.getElementById('Btn_change').addEventListener('click', change_array);
function change_array()
{
parent_interface.innerHTML = "";
}
btn_add = document.createElement('button');
btn_add.id = 'Btn_add';
btn_add.innerHTML = 'Hinzufügen';
btn_add.style = 'margin-right:12px;';
btn_add.type = 'button';
parent_interface.appendChild(btn_add);
document.getElementById('Btn_add').addEventListener('click', add_to_array);
function add_to_array()
{
parent_interface.innerHTML = "";
}
btn_delete = document.createElement('button');
btn_delete.id = 'Btn_delete';
btn_delete.innerHTML = 'Löschen';
btn_delete.style = 'margin-right:12px;';
btn_delete.type = 'button';
parent_interface.appendChild(btn_delete);
document.getElementById('Btn_delete').addEventListener('click', delete_from_array);
function delete_from_array()
{
parent_interface.innerHTML = "";
}
btn_sort = document.createElement('button');
btn_sort.id = 'Btn_sort';
btn_sort.innerHTML = 'Sortieren';
btn_sort.style = 'margin-right:12px;';
btn_sort.type = 'button';
parent_interface.appendChild(btn_sort);
document.getElementById('Btn_sort').addEventListener('click', sort_array);
function sort_array()
{
parent_interface.innerHTML = "";
}
btn_new = document.createElement('button');
btn_new.id = 'Btn_new';
btn_new.innerHTML = 'Neue Liste';
btn_new.style = 'margin-right:12px;';
btn_new.type = 'button';
parent_interface.appendChild(btn_new);
document.getElementById('Btn_new').addEventListener('click', new_array);
function new_array()
{
parent_interface.innerHTML = "";
h3_new_array = document.createElement('h3');
h3_new_array.textContent = 'Neue Liste';
h3_new_array.style = 'text-decoration:underline';
parent_interface.appendChild(h3_new_array);
p_new_array = document.createElement('p');
p_new_array.innerHTML = 'Wollen sie eine neue Lebensmittelgruppe erstellen , oder eine Einkaufsliste aus bestehenden Listen? ';
parent_interface.appendChild(p_new_array);
select_list_choice = document.createElement('select');
select_list_choice.id = 'Select_list_choice';
select_list_choice.autofocus = 'true';
p_new_array.appendChild(select_list_choice);
opt_Einkaufsliste = document.createElement('option');
opt_Einkaufsliste.value = 'Einkaufsliste';
opt_Einkaufsliste.id = 'Opt_einkaufsliste';
opt_Einkaufsliste.innerHTML = 'Einkaufsliste';
opt_Einkaufsliste.selected = 'selected';
select_list_choice.appendChild(opt_Einkaufsliste);
opt_new_list = document.createElement('option');
opt_new_list.value = 'new_list';
opt_new_list.id = 'Opt_new_list';
opt_new_list.innerHTML = 'Neue Lebensmittelgruppe';
select_list_choice.appendChild(opt_new_list);
p_list_name = document.createElement('p');
p_list_name.innerHTML = 'Name der Einkaufsliste : ';
parent_interface.appendChild(p_list_name);
var input_new_list_name = document.createElement('input');
input_new_list_name.type = 'text';
input_new_list_name.id = 'Input_new_list_name';
input_new_list_name.maxLength = "75";
input_new_list_name.style = 'margin-right:24px;';
p_list_name.appendChild(input_new_list_name);
btn_create = document.createElement('button');
btn_create.type = 'button';
btn_create.id = 'Btn_create';
btn_create.innerHTML = 'Speichern';
p_list_name.appendChild(btn_create);
select_list_choice_br = document.createElement('br');
parent_interface.appendChild(select_list_choice_br);
var section_lists = document.createElement('section');
parent_interface.appendChild(section_lists);
document.getElementById('Input_new_list_name').addEventListener('keydown', create_new_section);
var new_list_fieldset;
var keydown = 0;
document.getElementById('Btn_create').addEventListener('click',create_new_array);
document.getElementById('Input_new_list_name').addEventListener('keyup', insert_list_name);
function create_new_section()
{
if (keydown === 0)
{
new_list_fieldset = document.createElement('fieldset');
new_list_fieldset.style = 'display:inline-block; float:right;';
section_lists.appendChild(new_list_fieldset);
new_list_name = document.createElement('h5');
new_list_fieldset.appendChild(new_list_name);
keydown = 1;
}
}
function insert_list_name()
{
new_list_name.textContent = document.getElementById('Input_new_list_name').value;
}
function create_new_array ()
{
Arrays_push=document.getElementById('Input_new_list_name').value;
new_string=Arrays_push.toLowerCase();
new_user_array=Arrays_push.charAt(0).toUpperCase()+Arrays_push.slice(1);
new_user_array = [];
Arrays.push(new_string);
Arrays.push(new_user_array);
console.log(Arrays);
input_new_list_name.disabled = 'true';
}
document.getElementById('Select_list_choice').addEventListener('focus', new_array_interface);
document.getElementById('Select_list_choice').addEventListener('change', new_array_interface);
var new_list_section_child_counter = 0;
function new_array_interface()
{
var list_choice = document.getElementById('Select_list_choice').value;
if (new_list_section_child_counter !== 0)
{
section_lists.innerHTML = '';
new_list_section_child_counter = 0;
}
if (list_choice === 'Einkaufsliste')
{
for (let i = 0; i < Arrays.length; i = i + 2)
{
list_fieldset = document.createElement('fieldset');
list_fieldset.class = 'Theme_darkblue_fieldset';
list_fieldset.id = 'Fieldset_' + Arrays[i];
list_fieldset.style = 'display:inline-block;';
section_lists.appendChild(list_fieldset);
h5_select_from_lists = document.createElement('h5');
h5_select_from_lists.textContent = Arrays[i];
list_fieldset.appendChild(h5_select_from_lists);
select_from_lists = document.createElement('select');
select_from_lists.id = 'Select_' + Arrays[i];
select_from_lists.class = 'Select_lists';
list_fieldset.appendChild(select_from_lists);
document.getElementById('Select_'+Arrays[i]).addEventListener('change',selected_item);
list_fieldset_br = document.createElement('br');
list_fieldset_br2 = document.createElement('br');
list_fieldset.appendChild(list_fieldset_br);
list_fieldset.appendChild(list_fieldset_br2);
list_fieldset_btn = document.createElement('button');
list_fieldset_btn.innerHTML = 'Hinzufügen';
list_fieldset_btn.id = 'Btn_add_'+Arrays[i];
list_fieldset_btn.type = 'button';
list_fieldset.appendChild(list_fieldset_btn);
document.getElementById('Btn_add_'+Arrays[i]).addEventListener('click',add_item);
var list = Arrays[i + 1];
for (let i2 = 0; i2 < list.length; i2++)
{
opt = document.createElement('option');
opt.id = list[i2];
opt.value = list[i2];
opt.innerHTML = list[i2];
select_from_lists.appendChild(opt);
}
}
new_list_section_child_counter = 1;
function selected_item ()
{
selected_array_item = this.value;
};
console.log(selected_array_item);
var item_counter=0;
function add_item ()
{
new_user_array[item_counter]=selected_array_item;
item_counter++;
console.log(item_counter);
console.log[new_user_array];
}
}
}
}
};
So , i know thats a fucking shit-ton of code , and im already sorry for even asking this , but im trying to build a User controlled database,fully in javascript , im new to Html and yes , i do know that using html code , would make things much easyer to read , yet im doing this to improve my thought structures and workflow when using java-script.
The part im stuck at , at the moment (which somehow is the first time i really feel stuck) is the creation of a shopping list , the user can input a List name , which then shall be used as declaration for the Array , in which his later choices or inputs of items , are going to be saved.
creating the Array works fine , as console prints it´s there , the problem is , it´s an unnamed array , so , how can i declare it?
also is the missing declaration the reason for the variable selected_array_item not beeing saved within so said array? (note the last 2 functions)
I´d be super super super Thankfull if anyone can figure this out and explain in a way a beginner understands
greetings