Textarea does not want to clear (when button clicked) - javascript

I have just started out on learning JavaScript and HTML, and I am currently working on a pizza delivery program for a project. After the person gives an order (entering it into a form and clicking the order button) it is output into a text area.
If someone orders something else(click the order button again) the text area should be cleared, yet it does not clear. I have tried numerous things I found on SO and W3Schools, and yet it does not want to clear the text area.
Code to empty textarea:(Almost at the top lines within the orderPizzas function) The textarea is at the bottom of my code within the <body> with ID: Details
document.getElementById("Details").value = "";
Here is my code:
var pizzaCount = 0;
var gourmetPrice = 15.50;
var standardPrice = 9.50;
var deliveryCharge = 5;
var TotalPrice;
var name;
var adress;
var phoneNumber = 10;
var gourmetCount = 0;
var standardCount = 0;
var orderDetails = '';
function orderPizzas() {
var customerDetails;
var i = 0;
var j = 0;
TotalPrice = 0;
phoneNumber = '';
document.getElementById("Details").value = "";
var arrStandardPizza = new Array()
arrStandardPizza[0] = new Array();
arrStandardPizza[0]['name'] = 'Hawaiian';
arrStandardPizza[0]['amount'] = Number(document.standard.Hawaiian.value);
arrStandardPizza[1] = new Array();
arrStandardPizza[1]['name'] = 'Cheese';
arrStandardPizza[1]['amount'] = Number(document.standard.Cheese.value);
arrStandardPizza[2] = new Array();
arrStandardPizza[2]['name'] = 'Veggie';
arrStandardPizza[2]['amount'] = Number(document.standard.Veggie.value);
arrStandardPizza[3] = new Array();
arrStandardPizza[3]['name'] = 'Supreme';
arrStandardPizza[3]['amount'] = Number(document.standard.Supreme.value);
arrStandardPizza[4] = new Array();
arrStandardPizza[4]['name'] = 'Pepperoni';
arrStandardPizza[4]['amount'] = Number(document.standard.Pepperoni.value);
var arrGourmetPizza = new Array()
arrGourmetPizza[0] = new Array();
arrGourmetPizza[0]['name'] = 'Meatlovers';
arrGourmetPizza[0]['amount'] = Number(document.gourmet.Meatlovers.value);
arrGourmetPizza[1] = new Array();
arrGourmetPizza[1]['name'] = 'Chicken';
arrGourmetPizza[1]['amount'] = Number(document.gourmet.Chicken.value);
arrGourmetPizza[2] = new Array();
arrGourmetPizza[2]['name'] = 'Prawn';
arrGourmetPizza[2]['amount'] = Number(document.gourmet.Prawn.value);
standardCount = arrStandardPizza[0]['amount'] + arrStandardPizza[1]['amount'] + arrStandardPizza[2]['amount'] + arrStandardPizza[3]['amount'] + arrStandardPizza[4]['amount'];
gourmetCount = arrGourmetPizza[0]['amount'] + arrGourmetPizza[1]['amount'] + arrGourmetPizza[2]['amount'];
pizzaCount = standardCount + gourmetCount;
if (pizzaCount > 12) {
alert('A maximum of 12 pizzas can be ordered.\nPlease modify your order.\nPizzas ordered: ' + pizzaCount);
} else {
while (i < 5) {
if (arrStandardPizza[i]['amount'] > 0) {
orderDetails = orderDetails + '\n' + arrStandardPizza[i]['name'] + ': ' + arrStandardPizza[i]['amount'];
}
i++;
}
while (j < 3) {
if (arrGourmetPizza[j]['amount'] > 0) {
orderDetails = orderDetails + '\n' + arrGourmetPizza[j]['name'] + ': ' + arrGourmetPizza[j]['amount'];
}
j++;
}
if (document.getOrderMethod.method.value == 'Delivery') {
name = prompt('What is your name?');
adress = prompt('What is your adress?');
while (phoneNumber.toString().length !== 10) {
phoneNumber = prompt('What is your phone number?');
}
customerDetails = '\nDelivery:\n' + 'Name: ' + name + ' ' + '\n' + 'Adress: ' + adress + '\n' + 'Phone Number: ' + phoneNumber;
TotalPrice = deliveryCharge;
} else {
name = prompt('What is your name?');
customerDetails = '\nPick-up:\n' + 'Customer Name: ' + name;
}
TotalPrice = TotalPrice + (standardCount * standardPrice) + (gourmetCount * gourmetPrice);
orderDetails = orderDetails + customerDetails + '\n' + 'Total Cost: $' + TotalPrice;
document.getElementById("Details").value = orderDetails;
}
}
<!DOCTYPE html>
<html>
<head>
<title> Pete's Pizza </title>
</head>
<body>
<h1> Welcome to Pete's Pizzas, where the best pizzas are! </h1>
<h3> Enter your pizza order: </h3>
<label> Amount for each standard pizza </label>
<form name="standard">
<input type="text" name="Hawaiian"> Hawaiian Pizza <br>
<input type="text" name="Cheese"> Cheese Pizza <br>
<input type="text" name="Veggie"> Veggie Pizza <br>
<input type="text" name="Supreme"> Supreme Pizza <br>
<input type="text" name="Pepperoni"> Pepperoni Pizza <br>
</form>
<label> Amount for each gourmet pizza </label>
<form name="gourmet">
<input type="text" name="Meatlovers"> Meat-lovers Pizza <br>
<input type="text" name="Chicken"> Chicken Pizza <br>
<input type="text" name="Prawn"> Prawn <br>
</form>
<form name="getOrderMethod">
<input type="radio" name="method" value="Delivery" checked> Delivery <br>
<input type="radio" name="method" value="Pick-up"> Pick-up <br>
</form>
<input type="button" value="Confirm Order" onClick="orderPizzas()">
<input type="button" value="Cancel Order" onClick="window.location.reload()">
<textarea id="Details" value="" rows="9" cols="33" wrap=on readonly></textarea>
</body>
</html>
I am very new to JavaScript and HTML, all advice will be well-received. Thanks in advance.

It appears that the problem is because you have defined orderDetails outside your method. You need to clear the variable everytime or scope it locally, or you just keep appending to it.
Just move the variable declaration from global scope into the method definition and it ought to work.

You are not clearing the orderDetails.
orderDetails="";
in start of function "orderPizzas" use above code.

var pizzaCount = 0;
var gourmetPrice = 15.50;
var standardPrice = 9.50;
var deliveryCharge = 5;
var TotalPrice;
var name;
var adress;
var phoneNumber = 10;
var gourmetCount = 0;
var standardCount = 0;
function orderPizzas() {
var customerDetails;
var orderDetails = '';
var i = 0;
var j = 0;
TotalPrice = 0;
phoneNumber = '';
document.getElementById("Details").value = "";
var arrStandardPizza = new Array()
arrStandardPizza[0] = new Array();
arrStandardPizza[0]['name'] = 'Hawaiian';
arrStandardPizza[0]['amount'] = Number(document.standard.Hawaiian.value);
arrStandardPizza[1] = new Array();
arrStandardPizza[1]['name'] = 'Cheese';
arrStandardPizza[1]['amount'] = Number(document.standard.Cheese.value);
arrStandardPizza[2] = new Array();
arrStandardPizza[2]['name'] = 'Veggie';
arrStandardPizza[2]['amount'] = Number(document.standard.Veggie.value);
arrStandardPizza[3] = new Array();
arrStandardPizza[3]['name'] = 'Supreme';
arrStandardPizza[3]['amount'] = Number(document.standard.Supreme.value);
arrStandardPizza[4] = new Array();
arrStandardPizza[4]['name'] = 'Pepperoni';
arrStandardPizza[4]['amount'] = Number(document.standard.Pepperoni.value);
var arrGourmetPizza = new Array()
arrGourmetPizza[0] = new Array();
arrGourmetPizza[0]['name'] = 'Meatlovers';
arrGourmetPizza[0]['amount'] = Number(document.gourmet.Meatlovers.value);
arrGourmetPizza[1] = new Array();
arrGourmetPizza[1]['name'] = 'Chicken';
arrGourmetPizza[1]['amount'] = Number(document.gourmet.Chicken.value);
arrGourmetPizza[2] = new Array();
arrGourmetPizza[2]['name'] = 'Prawn';
arrGourmetPizza[2]['amount'] = Number(document.gourmet.Prawn.value);
standardCount = arrStandardPizza[0]['amount'] + arrStandardPizza[1]['amount'] + arrStandardPizza[2]['amount'] + arrStandardPizza[3]['amount'] + arrStandardPizza[4]['amount'];
gourmetCount = arrGourmetPizza[0]['amount'] + arrGourmetPizza[1]['amount'] + arrGourmetPizza[2]['amount'];
pizzaCount = standardCount + gourmetCount;
if (pizzaCount > 12) {
alert('A maximum of 12 pizzas can be ordered.\nPlease modify your order.\nPizzas ordered: ' + pizzaCount);
}
else {
while(i < 5) {
if ( arrStandardPizza[i]['amount'] > 0) {
orderDetails = orderDetails + '\n' + arrStandardPizza[i]['name'] + ': ' + arrStandardPizza[i]['amount'];
}
i++;
}
while(j < 3) {
if ( arrGourmetPizza[j]['amount'] > 0) {
orderDetails = orderDetails + '\n' + arrGourmetPizza[j]['name'] + ': ' + arrGourmetPizza[j]['amount'];
}
j++;
}
if (document.getOrderMethod.method.value == 'Delivery') {
name = prompt('What is your name?');
adress = prompt('What is your adress?');
while( phoneNumber.toString().length !== 10) {
phoneNumber = prompt('What is your phone number?');
}
customerDetails = '\nDelivery:\n' + 'Name: ' + name + ' ' + '\n' + 'Adress: ' + adress + '\n' + 'Phone Number: ' + phoneNumber;
TotalPrice = deliveryCharge;
}
else {
name = prompt('What is your name?');
customerDetails = '\nPick-up:\n' + 'Customer Name: ' + name;
}
TotalPrice = TotalPrice + (standardCount * standardPrice) + (gourmetCount * gourmetPrice);
orderDetails = orderDetails + customerDetails + '\n' + 'Total Cost: $' + TotalPrice;
document.getElementById("Details").value = orderDetails;
}
}
<h1> Welcome to Pete's Pizzas, where the best pizzas are! </h1>
<h3> Enter your pizza order: </h3>
<label> Amount for each standard pizza </label>
<form name ="standard">
<input type="text" name="Hawaiian" > Hawaiian Pizza <br>
<input type="text" name="Cheese" > Cheese Pizza <br>
<input type="text" name="Veggie" > Veggie Pizza <br>
<input type="text" name="Supreme" > Supreme Pizza <br>
<input type="text" name="Pepperoni" > Pepperoni Pizza <br>
</form>
<label> Amount for each gourmet pizza </label>
<form name ="gourmet">
<input type="text" name="Meatlovers" > Meat-lovers Pizza <br>
<input type="text" name="Chicken" > Chicken Pizza <br>
<input type="text" name="Prawn" > Prawn <br>
</form>
<form name="getOrderMethod">
<input type="radio" name="method" value="Delivery" checked> Delivery <br>
<input type="radio" name="method" value="Pick-up"> Pick-up <br>
</form>
<input type="button" value="Confirm Order" onClick="orderPizzas()" >
<input type="button" value="Cancel Order" onClick="window.location.reload()" >
<textarea id="Details" value="" rows="9" cols="33" wrap=on readonly>
</textarea>
CustomerDetails should be initialized every time the function is called.
Try it!

Related

How do I add up all numbers into one variable and show it?

Whenever I try doing the code myself, nothing shows up after I run the function, I'm wanting to add all the numbers from 'fkWynik'.
function mat_WypiszLiczbyNaturalne() {
var T = "";
T = document.getElementById('fkEdit').value;
if ((T.trim() != "") && (Number(T) > 0)) {
var S = "";
for (var I = 1; I < Number(T) + 1; I++) {
S = S + ", " + I.toString();
}
document.getElementById('fkWynik').value = S.substr(2) + " = ";
} else {
document.getElementById('fkWynik').value = "Prosze wprowadzic liczbe!";
}
}
<html>
<body>
<FORM NAME="formularz1" ACTION="">
<TABLE BORDER="0">
<INPUT TYPE="number" ID="fkEdit" STYLE="height:24px; width:55px;">
<INPUT TYPE="button" ID="fkWykonaj" VALUE="Wykonaj" onClick="mat_WypiszLiczbyNaturalne();"> </TD>
</TR>
<TR><TD> <INPUT TYPE="text" ID="fkWynik" STYLE="width:545px; height:24px;" READONLY> </TD></TR>
</TABLE>
</FORM>
</body>
</html>
You need to add the numbers together and concat it to the string:
function mat_WypiszLiczbyNaturalne() {
var T = "";
T = document.getElementById('fkEdit').value;
if ((T.trim() != "") && (Number(T) > 0)) {
var S = "";
var total = 0
for (var I = 1; I < Number(T) + 1; I++) {
S = S + ", " + I.toString();
total += I;
}
document.getElementById('fkWynik').value = S.substr(2) + " = "+total.toString();
} else {
document.getElementById('fkWynik').value = "Prosze wprowadzic liczbe!";
}
}
<html>
<body>
<FORM NAME="formularz1" ACTION="">
<TABLE BORDER="0">
<INPUT TYPE="number" ID="fkEdit" STYLE="height:24px; width:55px;">
<INPUT TYPE="button" ID="fkWykonaj" VALUE="Wykonaj" onClick="mat_WypiszLiczbyNaturalne();"> </TD>
</TR>
<TR><TD> <INPUT TYPE="text" ID="fkWynik" STYLE="width:545px; height:24px;" READONLY> </TD></TR>
</TABLE>
</FORM>
</body>
</html>
The following should work if your HTML code contains an element with the property id="fkWynik"
function mat_WypiszLiczbyNaturalne() {
var elem = document.getElementById('fkEdit');
if(!elem) {
console.error("No element with id 'fkEdit' in the page.");
return;
}
var T = elem.value;
var value = "";
if (T && !isNaN(Number(T))) { // if T not null, not empty, not undefined, not 0 and is a number
var S = "";
for (var I = 1; I < Number(T) + 1; I++) {
S = S + ", " + I.toString();
}
value = S.substr(2) + " = ";
} else {
value = "Proszę wprowadzić liczbę!";
}
console.log("value: " + value);
document.getElementById('fkWynik').value = value;
}
mat_WypiszLiczbyNaturalne();

How to print javascript while loop result in html textarea?

Prints '2 x 10 = 20' but not the whole table when the input is 2. I tried various means. But the result is same.
No error. Just like to print the whole multiplication table.
function loop() {
var i = 1;
var x = document.getElementById("num").value;
//document.getElementById("result").value = result;
while (i <= 10) {
document.getElementById("result").value = x + " x " + i + " = " + i * x;
i++;
}
}
<h1>While loop: </h1>
<p>The while loop keeps repeating an action until an associated condition returns false.</p>
<img src="images/loop.jpg" /><br/>
<img src="images/loop2.jpg" /><br/>
<body>
<p>JavaScripts Runs:</p>
<script src="while_1loop.js">
</script><br/> What number table would you like to have?<input type="number" name="" id="num" /><br>
<button type="button" onclick="loop()" ;>Start</button><br>
<textarea rows="12" cols="15" id="result" readonly>
</textarea><br/>
You are always changing the value of 'result' rather than adding to it:
function loop() {
var i = 1;
var x = document.getElementById("num").value;
//document.getElementById("result").value = result;
while (i <= 10) {
var result = document.getElementById("result");
var sum = document.createTextNode(x + " x " + i + " = " + i * x + "\n");
result.appendChild(sum);
i++;
}
}
<h1>While loop: </h1>
<p>The while loop keeps repeating an action until an associated condition returns false.</p>
<img src="images/loop.jpg" /><br/>
<img src="images/loop2.jpg" /><br/>
<body>
<p>JavaScripts Runs:</p>
<script src="while_1loop.js">
</script><br/> What number table would you like to have?<input type="number" name="" id="num" /><br>
<button type="button" onclick="loop()" ;>Start</button><br>
<textarea rows="12" cols="15" id="result" readonly>
</textarea><br/>
If I understand what you mean,
You rewrite whole textarea with this code:
document.getElementById("result").value = x + " x " + i + " = " + i * x;
but you need add new result after older results. Something like this:
var oldValue = document.getElementById("result").value;
var result = x + " x " + i + " = " + i * x;
document.getElementById("result").value = oldValue + '\n' + result;

Unable to generate a multiplication table with user input in JavaScript

I have a page which prompts the user to enter a positive integer from 1 to 9, then the javascript code will generate a multiplication table from the input value all the way to 9. I am getting an error in which I cannot retrieve the value and do a multiplication with it.
function timesTable()
{
var values = document.getElementById('value1');
var showTables = '';
for (var i=1; i<9; i++) {
showTables += values + " x " + i +" = "+ values*i + "\n";
}
var p_tables = document.getElementById('tables').innerHTML = showTables;
}
<label>Enter an integer from 1 to 9 : </label>
<input type="text" size=20 id=value1 name="value">
<button onclick="timesTable()">Generate times table</button><br> <br>
<p id="tables"></p>
Expected result:
You have to take the value of the element not the element itself
var values = document.getElementById('value1').value;
function timesTable()
{
var values = document.getElementById('value1').value;
var showTables = '';
for (var i=1; i<9; i++) {
showTables += values + " x " + i +" = "+ values*i + "<br>";
}
var p_tables = document.getElementById('tables').innerHTML = showTables;
}
<label>Enter an integer from 1 to 9 : </label>
<input type="text" size=20 id=value1 name="value">
<button onclick="timesTable()">Generate times table</button><br> <br>
<p id="tables"></p>
You are trying to multiply the element itself. What you actually want is the value.
function timesTable()
{
var values = document.getElementById('value1').value;
var showTables = '';
for (var i=1; i<9; i++) {
showTables += values + " x " + i +" = "+ values*i + "\n";
}
var p_tables = document.getElementById('tables').innerHTML = showTables;
}
<label>Enter an integer from 1 to 9 : </label>
<input type="text" size=20 id=value1 name="value">
<button onclick="timesTable()">Generate times table</button><br> <br>
<p id="tables"></p>
the javascript line in which you are trying to find value, is wrong as it will return the whole DOM and it's attributes and property.
You just have to find it's value, replace you line
var values = document.getElementById('value1');
with
var values = document.getElementById('value1').value;
This does what you want.
Note that if the user enters something unexpected, it may still fail. You can use an input of type="number" to require an integer (at least in some browsers.)
const userValue = document.getElementById("value1").value;
const p_tables = document.getElementById("tables");
let outputHtml = "";
for(let i = 1; i < 10; i++){
outputHtml += userValue + " x " + i + " = " + userValue * i + "<br/>";
}
p_tables.innerHTML = outputHtml;
you are using input field as text for table generation its better to use Number as input type and to get the value of input field you have to use value function as used in above code and for line break use
<\br>(please ignore '\').
function timesTable()
{
var values = document.getElementById('value1').value;
var showTables = '';
for (var i=1; i<=9; i++) {
showTables += values + " x " + i +" = "+ values*i + "<br>";
}
document.getElementById('tables').innerHTML = showTables;
}
<label>Enter an integer from 1 to 9 : </label>
<input type="Number" size=20 id=value1 name="value">
<button onclick="timesTable()">Generate times table</button><br> <br>
<p id="tables"></p>

AWS Javascript SDK EC2 DescribeInstances

I am trying to get date & no. of instances spin-up on that day.
Here is my code , i am beginner in development & in javascript.
so forgive me in advance for any immature code.
after getting startdate & end date , dates are stored in listDate array.
hours of a day is stored in hourArray.
then using JS SDK for AWS to get no. of instances spinned up on specific date.
ec2.describeInstances is not working as expected here ,
sometimes , i got output from ec2.describeInstances correctly , but it is always picking end date in all while loop iterations.
most of the time , ec2.describeInstances doesnt even execute.
function applyFilters() {
var listDate = [];
demo1.innerHTML = '';
var startDate = document.getElementById("fromdate").value;
var endDate = document.getElementById("todate").value;
//alert(startDate);
//alert(endDate);
var dateMove = new Date(startDate);
var strDate = startDate;
while (strDate < endDate){
var strDate = dateMove.toISOString().slice(0,10);
listDate.push(strDate);
dateMove.setDate(dateMove.getDate()+1);
};
//alert(listDate);
//document.getElementById('demo1').innerHTML = "Your selected dates are";
//demo2.innerHTML += "<br>";
//document.getElementById('demo3').innerHTML = listDate;
alert(listDate);
var i = 0;
var j = listDate.length;
alert(j);
while (i < j){
alert(i);
alert(listDate[i]);
var hourArray = [];
var d = listDate[i];
for (var h = 0; h <= 9; h++) {
hourArray.push(d + 'T' + '0' + h + '*');
}
for (var m = 10; m <= 23; m++) {
hourArray.push(d + 'T' + m + '*');
}
alert(hourArray);
var ec2 = new AWS.EC2();
AWS.config.update({
region: "xxxxxxx",
accessKeyId: "xxxxxxxxxxxxxxxxxxxxx",
secretAccessKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
});
i++;
var params = {
Filters: [
{
Name: 'launch-time',
Values: hourArray
}]
};
//alert("Specified launch-times are: " + hourArray);
ec2.describeInstances(params, function(err, data, d) {
//alert(data);
if (err) {
//demo5.innerHTML = 'ERROR:' + err;
console.log(err);
} else {
var no_of_inst = data.Reservations.length;
//demo4.innerHTML += "<br>";
//document.getElementById('demo5').innerHTML = 'Instances migrated on' + listdate[i] + 'are: ' + no_of_inst;
alert("Instances migrated on" + d + "are: " + no_of_inst);
}
});
}
}
<!DOCTYPE html>
<html>
<head>
<title>Migration Status</title>
<!--<link rel="stylesheet" href="styles.css">-->
<div>
<h3> M I G R A T I O N - S T A T U S </h3>
</div>
<meta charset="utf-8">
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.208.0.min.js"></script>
</head>
<body>
<p>Select Filters to get your ec2 graph :</p>
<form>
<div class = "css-grid1">
<label> Launch-time: </label>
</div>
<div class = "css-inlineblock1">
<label for="fromdate">From Date:</label>
<input type="date" id="fromdate" name="fromdate" min="2017-01-01">
<label for="todate">To Date:</label>
<input type="date" id="todate" name="todate">
</div>
<div class = "css-button">
<input type="submit" id="sbmt" onclick="applyFilters()">
</div>
<div id="demo1"></div>
<div id="demo2"></div>
<div id="demo3"></div>
<div id="demo4"></div>
<div id="demo5"></div>
<div id="demo6"></div>
<div id="demo7"></div>
<div id="demo8"></div>
</form>
<script>
</script>
</body>
</html>

Increment textarea name to javascript function

So what I am trying to achieve is to increment the points".$id." in the javascript code below starting from 0 like points+n and it would be a dynamic value according to rows in a table. Same for the value 'button".$id."' and all this is because of styled radiobutton labels that are looped etc.
So all I want to do is get rid of all the hardcoded different var txt1 to txt+n, button0 to button+n and points0 to points+n in the JavaScript function.
The real problem here for me is the line: var buttons1 = document.forms[0].button0; how to replace the 0 in button0 to the 'i' in a for loop. Someting like button + i won't work.
Oh and what I'm trying to do is get the values from the radiobuttons to a textarea with one buttongroup and textarea per tablerow.
The code below works for the first 7 rows in my table...
echo "
<td>
<div class='radio'>
<input id='".$temp1."' type='radio' name='button".$id."' onclick='myFunction()' value='4'>
<label for='".$temp1."'></label>
<input id='".$temp2."' type='radio' name='button".$id."' onclick='myFunction()' value='3'>
<label for='".$temp2."'></label>
<input id='".$temp3."' type='radio' name='button".$id."' onclick='myFunction()' value='2'>
<label for='".$temp3."'></label>
<input id='".$temp4."' type='radio' name='button".$id."' onclick='myFunction()' value='1'>
<label for='".$temp4."'></label>
</div>";
echo"<textarea id='points".$id."' name='points".$id."' cols='1' rows='1' ;> </textarea>
</td>
</tr>";
The Javascript function:
function myFunction() {
var txt1 ='';
var txt2 ='';
var txt3 ='';
var txt4 ='';
var txt5 ='';
var txt6 ='';
var txt7 ='';
var buttons1 = document.forms[0].button0;
var buttons2 = document.forms[0].button1;
var buttons3 = document.forms[0].button2;
var buttons4 = document.forms[0].button3;
var buttons5 = document.forms[0].button4;
var buttons6 = document.forms[0].button5;
var buttons7 = document.forms[0].button6;
var buttons8 = document.forms[0].button7;
for (var i = 0; i < 4; i++) {
if (buttons1[i].checked) {
txt1 = txt1 + buttons1[i].value + " ";
}
if (buttons2[i].checked) {
txt2 = txt2 + buttons2[i].value + " ";
}
if (buttons3[i].checked) {
txt3 = txt3 + buttons3[i].value + " ";
}
if (buttons4[i].checked) {
txt4 = txt4 + buttons4[i].value + " ";
}
if (buttons5[i].checked) {
txt5 = txt5 + buttons5[i].value + " ";
}
if (buttons6[i].checked) {
txt6 = txt6 + buttons6[i].value + " ";
}
if (buttons7[i].checked) {
txt7 = txt7 + buttons7[i].value + " ";
}
}
document.getElementById("points0").value = txt1;
console.log(txt1);
document.getElementById("points1").value = txt2;
console.log(txt2);
document.getElementById("points2").value = txt3;
console.log(txt3);
document.getElementById("points3").value = txt4;
console.log(txt4);
document.getElementById("points4").value = txt5;
console.log(txt5);
document.getElementById("points5").value = txt6;
console.log(txt6);
document.getElementById("points6").value = txt7;
console.log(txt7);
}
i think what you need is use the "eval" function in javascript
try the following
var buttons1;
var buttons2;
var buttons3;
var buttons4;
var buttons5;
var buttons6;
var buttons7;
var buttons8;
var j;
for(var i=0;i<8;i++){
j=i+1;
eval("buttons" +j+ " = document.forms[0].button" +i+ ";");
}
If i understood correctly,
Try something like this:
change your onclick as following:
<input id='".$temp4."' type='radio' name='button".$id."' onclick='myFunction(this)' value='1'>
and change function :
function myFunction(button){
var name= button.name;
var id= name.split('button')[1];;
document.getElementById("points"+id).value = buttons.value +" ";
}

Categories

Resources