how to work with elements inputted in forum after onsubmit | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to work with elements inputted in forum after onsubmit

how to make the elements inputted by user appear in the submit result for example in a forum contains a name input and submit button , how to make the name interred by the user appear in an alert after he click submit

1st Sep 2019, 12:08 PM
Ouardi Yacine
Ouardi Yacine - avatar
16 Answers
+ 1
Add an 'onsubmit' attribute to the form, then set the attribute's value to a JavaScript function. <form onsubmit="checkForm()"> And create the JavaScript function 'checkForm' (you are free to name the function but keep it sync with 'onsubmit' value). function checkForm() { alert("Name: " + Name.value); event.preventDefault(); return false; } Assuming `Name` was the Id of the text input. Mind that form data processing was supposed to be the back-end code task, this approach is more or less usable just to check input validity, where you can choose to allow form data submission or not. The `event.preventDefault();` is used to abort the form submission process, in this sample it is used because we only want to test alerting the name. Hth, cmiiw
1st Sep 2019, 2:08 PM
Ipang
+ 1
can u please just give me an example , for html and Js
1st Sep 2019, 2:15 PM
Ouardi Yacine
Ouardi Yacine - avatar
+ 1
Can you share your code so I can see it please? Also, which part of it wasn't clear? don't tell me you're giving up on this now ...
1st Sep 2019, 2:50 PM
Ipang
+ 1
i have another question , how can i use more than one item , for example above the alert will show name so how can i add for example age so it show name:expl , age:expl !
1st Sep 2019, 2:53 PM
Ouardi Yacine
Ouardi Yacine - avatar
+ 1
another question . can we style the document.write resulted page ? like a background...etc
1st Sep 2019, 2:58 PM
Ouardi Yacine
Ouardi Yacine - avatar
+ 1
Why do you want to use `document.write` can you explain me?
1st Sep 2019, 3:00 PM
Ipang
+ 1
It's possible if you use inline CSS to style the elements, but it was rather impractical, forget it altogether. Good job! and Thanks ✌
1st Sep 2019, 3:29 PM
Ipang
0
But I just did buddy, please try to make the changes I suggest, then share the code link, then if any problem I will try to help OK 👍
1st Sep 2019, 2:44 PM
Ipang
0
im sorry but its not clear to me because im useless at javascript
1st Sep 2019, 2:46 PM
Ouardi Yacine
Ouardi Yacine - avatar
0
no it worked now thanks
1st Sep 2019, 2:51 PM
Ouardi Yacine
Ouardi Yacine - avatar
0
i found it thanks
1st Sep 2019, 2:57 PM
Ouardi Yacine
Ouardi Yacine - avatar
0
insted of alert i used document.write because the message is long and now i want to know if i can style the message
1st Sep 2019, 3:02 PM
Ouardi Yacine
Ouardi Yacine - avatar
0
Okay, well, I can't say I would recommend that way, `document.write` rewrites the document from scratch, everything will be gone but what we write by that. Are you familiar with `document.getElementById()`function? and how to change element's text using `<element>.innerHTML` ? I would encourage you to lookup for these, by then you should be able to modify an element's text (to show name & age) without rewriting the document entirely.
1st Sep 2019, 3:13 PM
Ipang
0
thanks I've handled everything i just dont know if i can style the document.write result
1st Sep 2019, 3:23 PM
Ouardi Yacine
Ouardi Yacine - avatar
0
thanks
1st Sep 2019, 3:33 PM
Ouardi Yacine
Ouardi Yacine - avatar
1st Sep 2019, 4:19 PM
Ouardi Yacine
Ouardi Yacine - avatar