1

Roughly, this is my code:

<form name="mpform" id="mpform">
<input type="checkbox" onclick="mpfunc()" id="mpay" name="mpay[]" value="19" style="float:left;" />
//many of the above input tag - only the value changes with other numbers
</form>
<script type="text/javascript">
function mpfunc()
{
var comp=0;
document.getElementById("mpaydiv").style.visibility="visible";
for(var i=0; i< document.getElementById("mpform").elements['mpay[]'].length;i++){
if(document.getElementById("mpform").elements['mpay[]'][i].checked)
comp = comp + document.getElementById("mpform").elements['mpay[]'][i].value;
}
document.getElementById("mpaydiv").innerHTML=comp;
}
</script>
<div id="mpaydiv" style="position:fixed; right:0; visibility:hidden; bottom:0; border:1px solid black; background-color:white; padding:2px 2px;"></div></div>
    </div>

    <div id="footer"></div>
</div>

I have tried substituting getElementById("mpform") with form[0],mpform and others but I cant seem to access the values. With the current getelementid I get

Error: document.getElementById("mpform") is null Source File: index.php?page=fakturi Line: 3938

With mpform I get mpform is undefined as with form[0] Please help. How do I get the sum of the values of all selected checkboxes?

6 Answers 6

2

The id of your form must be mpform. You have name="mpform". Add id="mpform".

Also, for efficiency sake, you should change your function to this. Using multiple getElementById for grabbing the same element is very inefficient.

<script type="text/javascript">
function mpfunc()
{
   var comp=0;
   document.getElementById("mpaydiv").style.visibility="visible";
   var elements = document.getElementById("mpform").elements;
   for(var i=0; i< elements.length;i++){
      if(elements[i].type == 'checkbox' && elements[i].checked)
         comp = comp + parseInt(elements[i].value);
   }
   document.getElementById("mpaydiv").innerHTML=comp;
}
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, sorry I changed it. Same result though: Error: document.getElementById("mpform") is null Source File: index.php?page=fakturi Line: 3938
With the change of the function I get this: Error: document.getElementById("mpform") is null Source File: index.php?page=fakturi Line: 3938
I just edited my script. I believe the problem was the way you were accessing the elements. I just tested the script I just posted with your html code and it works now.
1

I seem to recall IE having issues with the 0 index of the forms array. You can access it through, document.getElementsByName("mpform")[0] or document.forms['mpform']. To get the checkbox descendants, you might use getElementById('mpay') or document.getElementsByName("mpay")[0] or document.getElementsByName("mpform")[0].elements[0]

3 Comments

Error: document.forms.mpform is undefined Source File: index.php?page=fakturi Line: 3938 Error: document.getElementsByName("mpform")[0] is undefined Source File: index.php?page=fakturi Line: 3938 I tried it even though I'm using FireFox
Are you sure that the form is actually there? What do you see when you do CTRL + U?
Thank you for making me look at my code. I had a previous form tag that wasn't closed.
1

Should be like this

     <form name="mpform" id="mpform"></form>

1 Comment

Yes, sorry I changed it. Same result though: Error: document.getElementById("mpform") is null Source File: index.php?page=fakturi Line: 3938
1

You try to get Element By Id but you specified a name for it.

<form id="mpform">

1 Comment

Yes, sorry I changed it. Same result though: Error: document.getElementById("mpform") is null Source File: index.php?page=fakturi Line: 3938
1

Is it possible that the function is running before the form element has finished loading? Try running the function as part of the window.onload event.

Comments

1

I just tried this out using your html code, and it seems to work. I edited my answer above to show this, but also posted it as another answer to make sure you saw.

<script type="text/javascript">
function mpfunc()
{
   var comp=0;
   document.getElementById("mpaydiv").style.visibility="visible";
   var elements = document.getElementById("mpform").elements;
   for(var i=0; i< elements.length;i++){
      if(elements[i].type == 'checkbox' && elements[i].checked)
         comp = comp + parseInt(elements[i].value);
   }
   document.getElementById("mpaydiv").innerHTML=comp;
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.