I am making greeting cards with user input using Javascript and CSS. I need to be able to have the information that the user submits in the form populate as a card underneath. I have some CSS assigned though this isnt the issue currently. I have a lot of the javascript bones there but I cant figure out how to return those elements once submitted.
let form = document.createElement('form');
form.setAttribute('id','card-form');
let nameInput = document.createElement('input');
nameInput.setAttribute('type','text');
nameInput.setAttribute('id','name-input');
nameInput.setAttribute('placeholder','Your Name');
nameInput.required = true;
form.appendChild(nameInput);
let messageTextArea = document.createElement('textarea');
messageTextArea.setAttribute('id','message-input');
messageTextArea.setAttribute('placeholder','Your Message');
messageTextArea.required = true;
form.appendChild(messageTextArea);
let imageInput = document.createElement('input');
imageInput.setAttribute('type','url');
imageInput.setAttribute('id','url-input');
imageInput.setAttribute('placeholder','Image or GIF URL');
imageInput.required = true;
form.appendChild(imageInput);
let submitButton = document.createElement('button');
submitButton.setAttribute('type','submit');
submitButton.innerText = 'Finish';
form.appendChild(submitButton);
document.body.appendChild(form);
let cardContainer = document.createElement('div');
cardContainer.setAttribute('id','card-container');
document.body.appendChild('cardContainer');
document.body.appendChild(form);
document.body.appendChild(div);
I tried simply returning the item on submit but this isnt returning anything
document.getElementById('card-form').addEventListener('submit', function (onSubmit) {
document.getElementById('card-container');
return div;
});
2
Answers
Couple of issues I see with this,
You’re not creating the card element itself and appending it to the cardContainer after the form submission.
Stop default behavior of the form submission to prevent the page from reloading.
First of all you have to fix the append container error in your code. you are appending string instead of the real container, so pay attention for that.
for the submit you can use this way to get the values:
you can do the same for the rest of the inputs.
then you can create you custom card to display the data in the way you want.
finally if you like to clear the inputs you can use :
I hope this answer your question