Page 1 of 1
adding textfield contents
Posted: Mon Oct 08, 2007 12:30 am
by shivam0101
Code: Select all
<input type='text' name='winning_amount[$match]' id=winning_amount>
Code: Select all
<script language="JavaScript">
function CalculatePrizeMoney()
{
frm=document.getElementById("final_result");
var txtelement=frm.getElementById("winning_amount");
alert(txtelement);
}
</script>
Code: Select all
<input type="text" name="total" Id='total'>
<input type="button" name="calculate" value="Total" onClick="CalculatePrizeMoney()">
i am trying to add the contents of winning_amount and display into a new textfield called total. I am getting error, object does not support
Posted: Mon Oct 08, 2007 1:06 am
by Miss_Alison
I would assume that the value of the text field would have to be set to display winning_amount.
Code: Select all
<input name="total" type="text" value="winning_amount">
Just a thought; not sure what you're trying to do, but in order for the field to originally display something without input, a value would need to be set.
Posted: Mon Oct 08, 2007 5:36 am
by shivam0101
In the database table, there is 2 filelds,
1. question
2. marks
I am creating an interface where the admin will enter marks to each question.
Code: Select all
$query=mysql_query("SELECT * FROM question");
while($fetch=mysql_fetch_array($query))
{
echo "$fetch[question] <input type='text' name='anwer[question]' id=answer>";
}
I also have a textbox which indicates the total marks. As the admin keeps on entering marks for each question, it will get updated (total=marks+total).
I tried the below code may be because its an array element i am not able to add.
I hope my question is clear. Can any one tell me how to add array elements in javascript
Re: adding textfield contents
Posted: Mon Oct 08, 2007 2:25 pm
by califdon
shivam0101 wrote:Code: Select all
<input type='text' name='winning_amount[$match]' id=winning_amount>
Code: Select all
<script language="JavaScript">
function CalculatePrizeMoney()
{
frm=document.getElementById("final_result");
var txtelement=frm.getElementById("winning_amount");
alert(txtelement);
}
</script>
Code: Select all
<input type="text" name="total" Id='total'>
<input type="button" name="calculate" value="Total" onClick="CalculatePrizeMoney()">
i am trying to add the contents of winning_amount and display into a new textfield called total. I am getting error, object does not support
I'm very confused by your code (such as, I've never seen an element name that contains square brackets--I don't know that that's wrong, but I've never seen it), but I think the error you're getting refers to the line where you defined "frm" as an HTML element with the Id "final_result", but then your syntax seems to be trying to call a function within that element, getElementById. I don't see what you're trying to do. Have you looked into the HTML attribute
innerHTML? You can have, say, a <DIV> or nearly any other closed element (one with a closing tag). and insert whatever you want as the content between the opening and closing tags by something like:
Code: Select all
getElementById("myId").innerHTML = "Hello, World"
Posted: Mon Oct 08, 2007 3:48 pm
by VladSun
Code: Select all
<script language="JavaScript">
<!--
function initBlurCapture()
{
for (i=1; el = document.getElementById('mark' + i); i++)
{
el.onblur = calcTotal;
}
}
function calcTotal()
{
var total = 0;
for (i=1; el = document.getElementById('mark' + i); i++)
{
if (val = parseInt(el.value)) total += val;
}
document.getElementById('total').value = total;
}
window.onload = initBlurCapture;
//-->
</script>
<input type="text" id="mark1"><br>
<input type="text" id="mark2"><br>
<br><br>
<input type="text" id="total">
Is it what you want to be like?
Posted: Sat Oct 13, 2007 11:09 pm
by shivam0101
Code: Select all
$query=mysql_query("SELECT * FROM question");
while($fetch=mysql_fetch_array($query))
{
$question_id=$fetch['question_id'];
echo "$fetch[question] <input type='text' name='marks[$question_id]' id=marks onChange='Calculate()'>";
}
echo "<input type='text' name='total' readonly>";
in Javascript,
Code: Select all
<script language="JavaScript">
function Calculate()
{
var sum=0;
for (i=0;i<document.form1.marks.length;i++) {
if(document.form1.marks[i].value=='')
document.form1.marks[i].value=0;
sum += parseInt(document.form1.marks[i].value);
}
document.getElementById("total").value = sum;
}
</script>
I tried the above code. It works fine only when the number of questions is more than 1. If it is only one question, then even on after i enter marks, the total textfield shows 0.