Forms&JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Forms&JS

I created form - test. It consists radiobutton, checkbox, select and text. I need check answer and show result. How?

4th May 2020, 1:10 PM
Polina T
Polina T - avatar
19 Answers
+ 6
Polina T Here's how you get the value from inputs using javascript. As you have used the 'javascript' tag, I assume this should be the answer :)) To get the values of the checked inputs : const elems = document.getElementByClassName('options'); const values = []; for (let e of elems) { if (e.checked) values.push(e.value); } Use the option class in your inputs like so : <input class="option" type="checkbox"> answer1 ...... To get the value from select : const value = null; const elem = document.getElementById('mySelect'); elem.onchange = () => { value = elem.value; } Use the 'mySelect' id in your select tag :)) I leave the radio button for you :)) However, I'll make a demo for you a bit later :))
4th May 2020, 1:55 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 4
Arb Rahim Badsa provided a nice explanation. If some supplements is needed, here is my video tutorial : https://youtu.be/vv5kIqCD45c
4th May 2020, 2:21 PM
Gordon
Gordon - avatar
+ 2
Hey Polina T, Share your code with us :)) It will help us know what you are trying to achieve!
4th May 2020, 1:13 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 2
Polina T If you don't know how to attach the code here, just copy the code and paste it :))
4th May 2020, 1:24 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 1
Arb Rahim Badsa she basically wants to get the data the user enters in the form and then do something based on the data.
4th May 2020, 1:28 PM
Sajid
Sajid - avatar
+ 1
sajid chowdhury If what you're saying is the case, then I think javascript can be used to grab the values from the input as well as doing something with them :))
4th May 2020, 1:37 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 1
<form .......> <p>1. Question <br> <input type="radio" name="fq" id="false-answer-f" value="f">answer1 <input type="radio" name="fq" value="true-answer-f">answer2 <p> 2. Question </p> <br> <input type="checkbox" name="sq" id="false-answer-s" value="f">answer1<br> <input type="checkbox" name="sq" value="true-answer-s1">answer2<br> <input type="checkbox" name="sq" id="false-answer-s" value="f">answer3<br> <input type="checkbox" name="sq" value="true-answer-s2">answer4 </p> <p>3. Question <br> <select> <option name="1">answer1 <option name="2">answer2 </select> </p> </form>
4th May 2020, 1:38 PM
Polina T
Polina T - avatar
+ 1
Part of this
4th May 2020, 1:38 PM
Polina T
Polina T - avatar
+ 1
Arb Rahim Badsa can you really do that with vanilla JavaScript? Didn't know that😞
4th May 2020, 1:39 PM
Sajid
Sajid - avatar
+ 1
And <input type="submit" value="send">
4th May 2020, 1:42 PM
Polina T
Polina T - avatar
+ 1
Thank everyone for your help 💛
4th May 2020, 2:50 PM
Polina T
Polina T - avatar
0
How, as well i know, html code isn't sending
4th May 2020, 1:18 PM
Polina T
Polina T - avatar
0
Polina T Do you want to get the data the user enters? If so, you need to work with the backend development
4th May 2020, 1:18 PM
Sajid
Sajid - avatar
0
Is it can be easier, just using "If" and value elements
4th May 2020, 1:22 PM
Polina T
Polina T - avatar
0
Polina T oh I understood your problem. But no you can't do that. You have to use backend like php.
4th May 2020, 1:25 PM
Sajid
Sajid - avatar
0
Polina T you should give the value attribute to the options inside select
4th May 2020, 1:46 PM
Sajid
Sajid - avatar
0
Yes, sure, and after this?
4th May 2020, 1:49 PM
Polina T
Polina T - avatar
0
<form .......> <p>1. Question <br> <input type="radio" name="fq" id="false-answer-f" value="f">answer1 <input type="radio" name="fq" value="true-answer-f">answer2 <p> 2. Question </p> <br> <input type="checkbox" name="sq" id="false-answer-s" value="f">answer1<br> <input type="checkbox" name="sq" value="true-answer-s1">answer2<br> <input type="checkbox" name="sq" id="false-answer-s" value="f">answer3<br> <input type="checkbox" name="sq" value="true-answer-s2">answer4 </p> <p>3. Question <br> <select> <option name="1">answer1 <option name="2">answer2 </select> </p> </form>
6th May 2020, 4:38 AM
Rushikesh Shivaji Bansode
Rushikesh Shivaji Bansode - avatar
- 3
Html
4th May 2020, 1:51 PM
Venky Venky
Venky Venky - avatar