- 1
Can anyone help me? Why only number appears in textarea when i chose two radio button in my code, second number replace frist.
6 Réponses
+ 1
Instead of text =
use text +=
but also close the input tags and only submit data to the text area on submit button.
Let me know if you need me to expand the answer.
HTH
+ 1
The traditional button method does a good job of using one function to combine radio button inputs. I have made some adjustments to a copy of your code below. The output is treated as two strings. if you want them added or used as numbers the use the parseInt() method. GL
<!DOCTYPE html>
<html>
<head>
<title>Number For Word</title>
<script>
function text(){
var name;
var school;
var names = document.getElementsByName('name');
for (var i = 0, length = names.length; i < length; i++) {
if (names[i].checked) {
name = names[i].value;
break; } }
var schools = document.getElementsByName('school');
for (var i = 0, length = schools.length; i < length; i++) {
if (schools[i].checked) {
school = schools[i].value;
break; } }
document.getElementById("my").value = (name + school );
};
</script>
</head>
<body>
<textarea id="my"></textarea>
<p>What is your name?</p>
<input type="radio" name="name" value ="1">Krio</input>
<input type="radio" name="name" value ="2">Ayman</input>
<br>
<input type="radio" name="name" value ="3">Mohamed</input>
<input type="radio" name="name" value ="4">Halim</input>
<input type="radio" name="name" value ="5">Mariam</input>
<br>
<p>What is your school?</p>
<input type="radio" name="school" value ="1" >Future</input>
<input type="radio" name="school" value ="2">Glil</input>
<input type="radio" name="school" value ="3" >Magdy</input>
<br>
<input type="radio" name="school" value ="4" >San Gorgre</input>
<input type="radio" name="school" value ="5">San Mary</input>
<br>
<button onClick=text()>ok</button>
</body>
</html>
+ 1
Thank you Arcayno
0
Thank you Arcayno but i don't want to use submit button now and can you show me the code of property text+=
0
Only numbers appears in textarea because you're using only number as 'value' attribute of your radio button ^^
Try something like :
<input type="radio" name="name" value ="krio" onclick="text(this.value)">krio
... others solution exists, but this is the simplest one in this case.
Anyway, @Arcayno advice is right if you doesn't want that a new selected choice delete previous output, you need at least modify your text functions as this:
function text(name) {
document.getElementById("my").value += name+'\n'; // obviously, do same in atext() function
}
... the new line character appended will make each new clicked choice appended to a new line of the textarea.
However, if user click more than once choice in each radio group, all clicked log will persist: better way to handle each of radio group choice in their own log output element ;)
0
Thank you visph