Anyone know why this javascript doesn't work?

E

Ed Jay

When I run the below js, I get the error:
"document.form1.elements[menu_name+(i)].checked) is null or not an object"

<script type="text/javascript">

function get_menu_value(menu_name) {

var mValue = 0;

for(var i=0; i < menu_name.length; i++) {

if (document.form1.elements[menu_name+(i)].checked) {

mValue = mValue + document.form1.elements[menu_name+(i)].value;
}
}
alert(mValue);
return false;
}
</script>

<form name = "form1">
Choice (11)<input type=checkbox name=choice value=11><br>
Choice (13)<input type=checkbox name=choice value=13><br>
Choice (15)<input type=checkbox name=choice value=15><br>
Choice (19)<input type=checkbox name=choice value=19><br>
Choice (100)<input type=checkbox name=choice value=100><br>
<input type = "submit" onclick="get_menu_value('choice')">
</form>
 
M

Michael Winter

On 16/12/2005 18:27, Ed Jay wrote:

[snip]
function get_menu_value(menu_name) {

var mValue = 0;

for(var i=0; i < menu_name.length; i++) {

Why does the length of the name determine the number of iterations of
this loop?
if (document.form1.elements[menu_name+(i)].checked) {

The expression in the square brackets will be evaluated, converted to a
string, and used as a property name. In your example, these names will
be 'choice0', 'choice1', ..., 'choice5'. Not only are there only five
checkboxes (you create six names), none of these names match a control
within the form, resulting in an undefined value, and an error with the
subsequent property access.
mValue = mValue + document.form1.elements[menu_name+(i)].value;

The value property is a string. The addition operator, with a string and
a number operand, will perform a concatenation. I suspect that you want
arithmetic addition.

[snip]
return false;
[snip]

<input type = "submit" onclick="get_menu_value('choice')">

As you do not return the value returned by the get_menu_value function,
it will have no effect. In any case, it is generally better to use the
submit event of the form, passing a reference to the form, rather than
the click event of the button.

function getMenuValue(menuName, form) {
var value = 0,
menu = form.elements[menuName];

for(var i = 0, n = menu.length; i < n; ++i) {
if(menu.checked) {
/* Unary plus performs explicit conversion to Number. */
value += +menu.value;
}
}
alert(value);

return false;
}

<form action="..." onsubmit="return getMenuValue('choice', this);">
<!-- ... -->
</form>

Hope that helps,
Mike
 
E

Ed Jay

Michael Winter said:
On 16/12/2005 18:27, Ed Jay wrote:

[snip]
function get_menu_value(menu_name) {

var mValue = 0;

for(var i=0; i < menu_name.length; i++) {

Why does the length of the name determine the number of iterations of
this loop?
if (document.form1.elements[menu_name+(i)].checked) {

The expression in the square brackets will be evaluated, converted to a
string, and used as a property name. In your example, these names will
be 'choice0', 'choice1', ..., 'choice5'. Not only are there only five
checkboxes (you create six names), none of these names match a control
within the form, resulting in an undefined value, and an error with the
subsequent property access.
mValue = mValue + document.form1.elements[menu_name+(i)].value;

The value property is a string. The addition operator, with a string and
a number operand, will perform a concatenation. I suspect that you want
arithmetic addition.

[snip]
return false;
[snip]

<input type = "submit" onclick="get_menu_value('choice')">

As you do not return the value returned by the get_menu_value function,
it will have no effect. In any case, it is generally better to use the
submit event of the form, passing a reference to the form, rather than
the click event of the button.

function getMenuValue(menuName, form) {
var value = 0,
menu = form.elements[menuName];

for(var i = 0, n = menu.length; i < n; ++i) {
if(menu.checked) {
/* Unary plus performs explicit conversion to Number. */
value += +menu.value;
}
}
alert(value);

return false;
}

<form action="..." onsubmit="return getMenuValue('choice', this);">
<!-- ... -->
</form>

Hope that helps,

It sure does, Mike. Thank you very much. I'm using your solution for menus
where all checkboxes have the same name. (I'm going to use Evertjan's for
those menus where the names are different, e.g., choice1, choice2...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top