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
16 Respostas
+ 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
+ 1
can u please just give me an example , for html and Js
+ 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 ...
+ 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 !
+ 1
another question . can we style the document.write resulted page ? like a background...etc
+ 1
Why do you want to use `document.write` can you explain me?
+ 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 ā
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 š
0
im sorry but its not clear to me because im useless at javascript
0
no it worked now thanks
0
i found it thanks
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
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.
0
thanks I've handled everything i just dont know if i can style the document.write result
0
thanks
0
heres the code
https://code.sololearn.com/WphIr15266OQ/?ref=app