+ 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?

31st Jan 2019, 8:35 PM
Grasshopper Hell Monkey
Grasshopper Hell Monkey - avatar
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!
31st Jan 2019, 9:24 PM
Maksat Orazsahedow
Maksat Orazsahedow - avatar
+ 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.
31st Jan 2019, 9:33 PM
Maksat Orazsahedow
Maksat Orazsahedow - avatar
+ 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.
31st Jan 2019, 11:05 PM
James
James - avatar
+ 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/
31st Jan 2019, 11:09 PM
James
James - avatar
+ 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.
31st Jan 2019, 11:14 PM
James
James - avatar
0
Thanks, if i got it right, inp is like a variable? Also, i can do the same with which other values?
31st Jan 2019, 9:26 PM
Grasshopper Hell Monkey
Grasshopper Hell Monkey - avatar
0
It was exactly what i wanted to know Maksat, and thanks James for the explanation, and sure, I'll try it out.
1st Feb 2019, 3:14 AM
Grasshopper Hell Monkey
Grasshopper Hell Monkey - avatar