+ 3
Input HTML to JS
How do i transfer the user input i may acquire with HTML to the JS code? Please, give some examples. Also, what's that server-side thing i heard about in PHP/HTML discussions?
7 Answers
+ 6
You can get users input through DOM using this code:
HTML codes
<input type="text" id="inp"/>
JS codes
var val = document.getElementById("inp").value;
I hope, This might be useful for you!
+ 2
No inp isn't a variable. It's id that represents input.
Yes, you can acces users value through input's id! If you explain what do you want to do exactly I might help you.
+ 1
Instead of getting each input by it's ID, you can select the whole form at once.
HTML:
<form id="myForm">
<input name="person" />
<input name="age" />
</form>
JS:
var myForm = document.getElementById("myForm");
document.write(myForm.person.value + " is " + myForm.age.value + " years old.");
Also, you should put the JS inside a function, which is triggered by onclick or onchange.
+ 1
PHP is useful, because unlike JS, it can save the form input to a database. You can find the PHP lesson on forms here, though you may need to learn PHP basics first...
https://www.sololearn.com/learn/PHP/1840/
+ 1
To answer your question about it being a variable...
Pathways to HTML elements can be placed in variables, not elements themselves per se.
//To place the pathway into the variable
var myInput = document.getElementById("myInput");
//To get it's current value
var myValue = myInput.value;
//To assign it's value
myInput.value = 5;
NOTE: you can't place a pathway to .value into a variable. It will place the value itself into the variable instead. You must call .value every time you want to use it.
0
Thanks, if i got it right, inp is like a variable?
Also, i can do the same with which other values?
0
It was exactly what i wanted to know Maksat, and thanks James for the explanation, and sure, I'll try it out.