There are a few ways to do this. As you are a beginner, I will put the most "simple" (I found) way to do this, but not the best.
First, let’s look at a functional example:
<script>
function PreencherCampo(){
var pergunta1 = document.getElementById("pergunta1").innerHTML;
var pergunta2 = document.getElementById("pergunta2").innerHTML;
var elementos1 = document.getElementsByName('pergunta1');
var resposta1 = '';
for (i = 0; i < elementos1.length; i++) {
if (elementos1[i].checked) {
resposta1 = elementos1[i].value;
}
}
var elementos2 = document.getElementsByName('pergunta2');
var resposta2 = '';
for (i = 0; i < elementos2.length; i++) {
if (elementos2[i].checked) {
resposta2 = elementos2[i].value;
}
}
document.getElementById("resposta").innerHTML = pergunta1 + ': ' + resposta1 + '\n'+
pergunta2 + ': ' + resposta2;
}
</script>
<p id="pergunta1">Pergunta1?</p>
<input type="radio"name="pergunta1"value="Sim" checked/>SIM
<input type="radio"name="pergunta1"value="Não"/>Não
<p id="pergunta2">Pergunta2?</p>
<input type="radio"name="pergunta2"value="Sim" checked/>SIM
<input type="radio"name="pergunta2"value="Não"/>Não
<br>
<p>
<input type="submit"value="Enviar" onclick="PreencherCampo()"/>
</p>
<br>
<p>
<textarea name"mensagem" rows="10" cols="27" id="resposta"></textarea>
</p>
In this example, first we take the value of each question by id
of the elements, thus:
var pergunta1 = document.getElementById("pergunta1").innerHTML;
var pergunta2 = document.getElementById("pergunta2").innerHTML;
After that, we’ll get all the radios
for name
, and after that check which one is with the attribute checked
(indicating which is marked or not marked).
var elementos1 = document.getElementsByName('pergunta1');
var resposta1 = '';
for (i = 0; i < elementos1.length; i++) {
if (elementos1[i].checked) {
resposta1 = elementos1[i].value;
}
}
And we do the same thing with the second group, as the example code shows. If we had more, we would create the third and so on.
After that, we need to put the value on textarea
, and for that just get the element (textarea) by id
and set the innerHTML
as the value we want, so:
document.getElementById("resposta").innerHTML = pergunta1 + ': ' + resposta1 + '\n'+
pergunta2 + ': ' + resposta2;
Finally, we call our function by clicking the button send, by the event .onclick() on our button, in this way:
<input type="submit"value="Enviar" onclick="PreencherCampo()"/>
With all this, we have the expected result, using only javascript.
knows how to use Jquery?
– user3010128
I don’t know man. I started developing for web a little while ago.
– Gabriel
It would help?
– Gabriel
Dude, it’s super simple stuff, but it requires medium knowledge, if you don’t put all the code here and it still won’t work, take a look at this: http://api.jquery.com/
– user3010128
Could you tell me which of these tags I use to do what I need? Knowing which tag is the right one I turn to see how it is implemented in the code.
– Gabriel
Our Renan and Randrade, helped a lot, the two worked and ran perfectly, and gave to understand cool step by step how it works with your explanation, helped a lot, thank you very much!
– Gabriel
I believe that this question is not appropriate to the proposal of the website because it does not present a technical doubt, but asks for a complete solution to a problem to be provided. Please note the rules for posting questions in the website’s Help Center.
– Caiuby Freitas